c# - WebService based on an interface for mocking -


question background:

i have 2 separate projects. 1 consists of class , test project, other web service. want implement mocking against web service.

question:

i have web reference web service called 'webservice' being consumed class 'proxyhandler', shown:

private webservice _webserviceobject   public proxyhandler(webservice webserviceobject)     {         _webservice = webserviceobject;     } 

the web service class implements interface, shown

public class webserviceclass:iwebservice 

which implementation?:

  1. can web reference of type 'iwebservice'?

  2. or, need implement new class based on interface consumes webservice, class implemented proxy? allow me mock against interface, 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>(); 

you abolutely can. below example created using service reference created in visual studio. in example uszipsoap web service interface , uszipsoapclient implementing class. comes out of box. need configuration service, below demonstrates how use factory method unity resolve 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 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 unit tests 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");      } } 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -