How-to Mock WCF Services Proxies with Rhino Mocks ?
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.
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.
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With