简体   繁体   中英

How to Unit Test a SoapExtension derived class?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM