unit test a method that calls wcf service

前端 未结 2 1908
你的背包
你的背包 2021-02-09 06:20

How do I unit test a Business Layer method that makes call to WCF service?

example:

public void SendData(DataUnit dataUnit)
{

        //this is WCF call         


        
2条回答
  •  时光说笑
    2021-02-09 06:54

    You should separate your service call from your business layer:

    Using the demo below, your Business Layer method that you listed would now look like this:

    public void SendData(IMyInterface myInterface, DataUnit dataUnit)
    {
    
            myInterface.SomeMethod(dataUnit);
    
    }
    

    Pass in a RealThing if you want to do the service call, pass in a TestThing if you just want to run a test:

    public interface IMyInterface
    {
       void SomeMethod(DataUnit x);
    }
    
    public class RealThing : IMyInterface
    {
       public void SomeMethod(DataUnit x)
       {
           SomeServiceClient svc = new SomeServiceClient();
           svc.SomeMethod(x);
       }
    }
    
    public class TestThing : IMyInterface
    {
       public void SomeMethod(DataUnit x)
       {
           // do your test here
       }
    }
    

提交回复
热议问题