开发者

Mocking Web Services for client layer unit testing

I have a business rule visual studio class library (.NET 2.0) project that takes a dependency on Dynamics Crm Web Services - a classic SOAP web reference as opposed to a WCF endpoint. I want to unit test those business rules without having a real crm instance behind it. Adding a web reference doesn't produce an interface that I can fake. It does generate c# in my project that I think I can fake if I can create the interface. I don't think I need to actually navigate HTTP and get into all of the protocol stuff.

开发者_开发知识库

I saw Joh Skeet's blog post. Alas I didn't want to write any code and I'm hoping a tool has been written since then that might help. I tried some of his steps but concluded that he is smarter than me and I couldn't make that work.

I am aware of SoapUI, however, I was hoping for pure unit tests that would work in a CI build environment.

Is there a way to do this.


The standard way to mock something which doesn't come with an interface, is to build your own wrapper around it.

the code you want to mock, say the webservice stuff:

class AutoGeneratedStuff
{
    public string GeneratedMethodYouUse()
    {...}
    public string GeneratedMethodYouDontNeed()
    {...}
}

you then make an interface which covers only the bits of the code you need:

public interface IWebServiceClient
{
    string MethodYouUse();
}

and a concrete wrapper class which implements it, which has a dependency to the generated stuff

class WebServiceClient : IWebServiceClient
{
    private AutoGeneratedStuff _stuff;

    public WebService(AutogeneratedStuff stuff)
    {
         _stuff = stuff;
    }

    public string MethodYouUse()
    {
        return _stuff.MethodYouUse();
    }
}

then, in your code when you would have called the generated class, call your interface instead. In your unit tests, you can mock the interface, either using a mocking framework, or by implementing the interface with another concrete class that has no dependencies to the generated stuff

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜