Question

Question background:

I have two separate projects. One consists of class and a test project, the other a web service. I want to implement mocking against the web service.

Question:

I have a web reference to the web service called 'webService' being consumed by the class 'ProxyHandler', as shown:

private webService _webServiceObject

 public ProxyHandler(webService webServiceObject)
    {
        _webService = webServiceObject;
    }

The web service class implements an interface, as shown

public class WebServiceClass:IWebService

Which Implementation?:

  1. Can I get the web reference to be of the type 'IWebService'?

  2. Or, do I need to simply implement a new class based on an interface that consumes the webservice, then this class itself is implemented by the proxy? This would then allow me to mock against the interface, as shown:

Modified Proxy class:

 private webService _webServiceHandlerObject;

 public ProxyHandler(IwebServiceHandler webServiceHandlerObject)
    {
        _webServiceHandlerObject = webServiceHandlerObject;
    }

Added 'WebServiceHandlerObject:

private webService _webServiceObject;

public Class WebServiceHandler:IwebServiceHandler

public WebServiceHandler(webService webServiceObject)
    {
       _webServiceObject = webServiceObject;
    }

To give:

var proxy = new ProxyHandler(new WebServiceHandler(new webService()));

var mockedProxy = newMock<IwebServiceHandler>();
Was it helpful?

Solution

You abolutely can. Below is an example that I created using a service reference created in Visual Studio. In this example USZipSoap is the web service interface and USZipSoapClient is the implementing class. This all comes out of the box. Often you will need to do some configuration to your service, so below demonstrates how to use a factory method with Unity to resolve your dependencies.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClassLibrary3.MyService;
using Microsoft.Practices.Unity;

namespace ClassLibrary3
{
    public class ProxyHandler
    {
        public USZipSoap iwebService { get; set; }
        public ProxyHandler(USZipSoap iWebService)
        {
            this.iwebService = iWebService;
        }

        public string GetZipInfo()
        {
            return iwebService.GetInfoByZIP("20008").InnerXml;
        }

        public static IUnityContainer BootstrapContainer()
        {
            var container = new UnityContainer();

            //Simple Registration
            //container.RegisterType<USZipSoap, USZipSoapClient>("Simple", new InjectionConstructor(new object[0]));

            //Factory registration
            container.RegisterType<USZipSoap>(new InjectionFactory(c => ProxyHandler.CreateSoapClient()));

            return container;
        }


        public static USZipSoap CreateSoapClient()
        {
            var client =  new USZipSoapClient();

            /*Configure your client */

            return client;
        }

        public static void Main()
        {
            var container = ProxyHandler.BootstrapContainer();


            var proxy2 = container.Resolve<USZipSoap>();
            var proxy1 = container.Resolve<ProxyHandler>();


            Console.WriteLine(proxy1.GetZipInfo());
            Console.ReadLine();
        }
    }    
}

Then your unit tests would look like this with Moq:

[global:Microsoft.VisualStudio.TestTools.UnitTesting.TestClass]
public class MyTestClass
{
    [global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethod]
    public void MyTestMethod()
    {
        //Arrange
        var mock = new Mock<USZipSoap>();
        var proxy = new ProxyHandler(mock.Object);

        //Act
        var result = proxy.GetZipInfo();

        //Assert
        mock.Verify(m => m.GetInfoByZIP("20008"), Times.Once, "Error");

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top