Easy way to mock a WCF service?

早过忘川 提交于 2019-12-18 11:33:11

问题


I've got an app which is using a WCF service. Now I'd like to add unit tests to the app.

For some cases I need to mock the WCF service, since getting the desired behaviour from the service sometimes is tough (e.g. service throws special exceptions).

I could add yet another interface to the wcf client, but this seems a bit silly, since the client calls already are using an interface.

Is there an easy way to mock a WCF service? Easier than creating another interface layer and redirecting every single WCF call inside it?

Edit: Most of the answers seem not to know much about WCF service using, so some clarification:
To use a WCF service from a ViewModel, I have to manage the connection something like this:

ChannelFactory<IMyWcfService> channelFactory = new ChannelFactory<IMyWcfService>("");
IMyWcfService proxy = channelFactory.CreateChannel();
proxy.CallMyStuff();
proxy.Close();

I can't just pass the ViewModel the proxy to the WCF, since the connection needs to be opened and closed for every transaction. For this reason using RhinoMock/NMock would not work, since they need a ViewModel which gets the proxy as a parameter, which can't be done like this if you use WCF.


回答1:


Why can't you use something like NMock2 to mock the IMyWcfService interfaces directly?

If you need to be able to create new instances on the fly, use the Factory to hide the ChannelFactory<IMyWcfService> from the client. This way you can replace the factory, providing the client one which creates mocks instead of real proxies.




回答2:


You can use any mocking framework like RhinoMocks or NMock, to mock out the interface contract, so if your service implemented IMyService then you could use a mocking framework to set expectations on the method calls on that interface. If you are not familiar with this concept then you can simply create a stand-in object that implements IMyService but pretends to be the real service during your testing. This way when the methods are called they are called on your stand-in object and you can have your stand-in return whatever you want it to.




回答3:


You can you Moq mocking framework. Based on the example that you have provided:

ChannelFactory<IMyWcfService> channelFactory = new ChannelFactory<IMyWcfService>("");
IMyWcfService proxy = channelFactory.CreateChannel();
proxy.CallMyStuff();
proxy.Close();

Here is how a mocking implementation will look like:

Mock<IMyWcfServiceChannel> channelMock = new Mock<IMyWcfServiceChannel>(MockBehavior.Strict);
channelMock
    .Setup(c => c.CallMyStuff())
    .Returns("");

string myStuff = channelMock.Object.CallMyStuff();

After you have added a proxy for the WCF service - you should have a channel interface available to you, called IMyWcfServiceChannel.

Depending on the return type of service method you are calling - you can set just about any output. In the example above I used string type as an example.

In order to use the above solution more efficiently you might want to create 2 constructors for the business layer like so:

public class Example1
{
    IMyWcfServiceChannel _client;

    public Example1()
    {
        var factory = new ChannelFactory<IMyWcfServiceChannel>("binding");
        _client = factory.CreateChannel();
    }

    public Example1(IMyWcfServiceChannel client)
    {
        _client = client;
    }

    public string CallMyStuff()
    {
        return _client.CallMyStuff();
    }
}

So on the prod you use parameter-less constructor. In unit tests you use parameter-full constructor and pass mock to it (channelMock.Object).



来源:https://stackoverflow.com/questions/406955/easy-way-to-mock-a-wcf-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!