Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How-To Mock WCF Services?

How-to Mock WCF Services Proxies with Rhino Mocks ?

like image 481
Yoann. B Avatar asked Jan 10 '09 17:01

Yoann. B


People also ask

How to mock WCF service?

The async generated methods are not part of your service methods and is created by WCF as part of the Client class. To mock those as an Interface do the following: Extract the Interface of the ContactServiceClient class. In VS it's simply a right-click (on the class name), refactor, extract interface.

Why do we mock a service?

Mock Services help you handle situations where a piece of your application might not be available for testing when you need it. When combined with BlazeMeter performance testing functionality, Mock Services can make performance testing easier and more powerful.

What to unit test?

Unit testing is a software development process in which the smallest testable parts of an application, called units, are individually and independently scrutinized for proper operation. This testing methodology is done during the development process by the software developers and sometimes QA staff.


2 Answers

Create your service so that it implements an interface. Then you can either mock the interface in your client or create a fake implementation of the interface to use in your tests.

like image 122
tvanfosson Avatar answered Oct 22 '22 00:10

tvanfosson


I know this is an old post but it something that I have recently done. In my scenario I have some Acceptance Tests which talk to a Wcf Service but I didn't want to use the real service. I did actually do a blog post on it but here is the low down.

A static class for creating a wcf service for a given object:

public static class MockServiceHostFactory
{
    public static ServiceHost GenerateMockServiceHost<TMock>(TMock mock, Uri baseAddress, string endpointAddress)
    {
        var serviceHost = new ServiceHost(mock, new[] { baseAddress });

        serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
        serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode = InstanceContextMode.Single;

        serviceHost.AddServiceEndpoint(typeof(TMock), new BasicHttpBinding(), endpointAddress);

        return serviceHost;
    }
}

Creating a mock and using it as a wcf service:

Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add<ServiceContractAttribute>();

var myWcfServiceMock = Substitute.For<IMyWcfService>();
var mockServiceHost = MockServiceHostFactory.GenerateMockServiceHost(myWcfServiceMock , new Uri("http://localhost:8001"), "MyService");
mockServiceHost.Open();
...
mockServiceHost.Close();
like image 41
Bronumski Avatar answered Oct 22 '22 00:10

Bronumski