2

I have made a code to read the external web service and find the expose methods and their parameters using System.Web.Services.ServiceDescription. I am also able to invoke the method and get the output through webservice. This is done only on the bases of external Web service Url.

Everything is done from CodeBehind (C#).

I need to add the unit test case to test the functionality by adding dummy .asmx webservice which will be accessed by unit test.

Please let me know or have suggestion to how can i create a dummy service on the fly and used.

Sunny Milenov
  • 20,782
  • 5
  • 75
  • 102
Shivkant
  • 4,089
  • 1
  • 17
  • 13
  • you should look into [mocking](http://stackoverflow.com/a/2666006/390819). There are [numerous frameworks](http://stackoverflow.com/q/37359/390819) out there – Cristian Lupascu Sep 03 '13 at 06:15
  • If you are calling a service, then you are not performing a unit test. That's a functional test, or possibly an integration test. What exactly are you trying to test? – John Saunders Sep 03 '13 at 20:48

1 Answers1

0

As far as I can see, there 2 different functionalists:

WSDL provider - i.e. class which gets a valid wsdl from somewhere WSDL parser - the class which parses the wsdl and extracts the data

Here is a pseudo-code implementation of these to make them easy to mock and unit test.

public interface IWSDLProvider
{
   string GetWsdlFromService(string url);
}

public class MyWsdlProvider : IWSDLProvider
{
   private readonly IWebWrapper _webCLient;

   public MyWsdlProvider(IwebWrapper webClient)
   {
       _webClient = webCLient;
   }

   public string GetWsdlFromService(string url)
   {
      //do here whatever is needed with the webClient to get the wsdl
   }
}

public interface IWSDLParser
{
   MyServiceData GetServiceDataFromUrl(string url);
}

public class MyWsdlParser : IWSDLParser
{
   private readonly IWSDLProvider _wsdlProvider;
   public MyWsdlParser(IWSDLProvider wsdlProvider)
   {
      _wsdlProvider = wsdlProvider;
   }

   public MyServiceData GetServiceDataFromUrl(string url)
   {
      //use the wsdl provder to fetch the wsdl
      //and then parse it
   }
}

The IWebClient is a wrapper around WebClient to allow easy mocking.

Using any mocking framework with the above code, you can easily isolate and mock any part, and test only the behavior at hand. That way, you can even make the mock for the wsdl provider to return any wsdl you want to test with.

You can go even further and wrap/isolate the System.Web.Services.ServiceDescription calls, so you don't really have to pass even a wsdl in your tests, just work on the results.

Sunny Milenov
  • 20,782
  • 5
  • 75
  • 102