如何对 SoapExtension 派生类进行单元测试? [英] How to Unit Test a SoapExtension derived class?
问题描述
我有一个源自 SoapExtension 的类.例如,要对 ProcessMessage(SoapMessage) 方法进行单元测试,我需要输入一个 SoapMessage,它是一个抽象类.当我尝试这样做时,我收到一条错误消息,说它没有构造函数.即使我要创建一个派生自 SoapMessage 的新类,我也无法创建自己的构造函数.我不能用模拟绕过这个,因为我需要能够设置 SoapMessage.Stage 属性,以便 ProcessMethod 可以运行它的 switch 语句,但该属性是只读的.我如何获得我自己的 SoapMessage 派生类,我可以设置 .Stage 属性,或者是不可能的,因此不可能进行单元测试?
I have a class deriving from SoapExtension. To unit test, for example, the ProcessMessage(SoapMessage) method, I need to input a SoapMessage, which is an abstract class. When I try doing this, I get an error saying it has no constructors. Even if I were to create a new class deriving from SoapMessage, I can't create my own constructor. I can't bypass this with a mock because I need to be able to set the SoapMessage.Stage property so that the ProcessMethod can run it's switch statement, but that property is readonly. How do I get my own SoapMessage derived class that I can set the .Stage property or is it not possible and thereofore not possible to unit test?
示例:
public override void ProcessMessage(SoapMessage message)
{
switch (message.Stage)
{
case SoapMessageStage.BeforeSerialize:
break;
case SoapMessageStage.AfterSerialize:
WriteOutput( message );
break;
case SoapMessageStage.BeforeDeserialize:
WriteInput( message );
break;
case SoapMessageStage.AfterDeserialize:
break;
}
}
您可以在 MSDN 上看到对 SoapMessage 的解释:http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapmessage.aspx
You can see an explanation of SoapMessage from MSDN: http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapmessage.aspx
推荐答案
有一些类型是从 SoapMessage
派生的,但都有内部构造函数.我找不到创建 SoapMessage
的 API,因此您可能需要使用反射来创建类型,如下所示.
There are some types that derive from SoapMessage
, but all have internal constructors. I couldn't find an API to create the SoapMessage
so you may need to resort to using reflection to create the type as follows.
SoapMessage CreateSoapClientMessage()
{
return (SoapClientMessage)Activator.CreateInstance(
typeof(SoapClientMessage),
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new object[] { null, null, null },
null);
}
这篇关于如何对 SoapExtension 派生类进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!