How to Unit Test a SoapExtension derived class?

混江龙づ霸主 提交于 2020-01-24 16:02:09

问题


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?

Example:

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;
  }
}

You can see an explanation of SoapMessage from MSDN: http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapmessage.aspx


回答1:


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);
} 


来源:https://stackoverflow.com/questions/6302177/how-to-unit-test-a-soapextension-derived-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!