Search code examples
c#.netwcfmicrosoft-fakes

Microsoft Shims with a WCF Service


I am attempting to follow this example and leverage a shim to remove the external dependency on a WCF service call which is called from the method I am executing the unit test on. Unlike the example, I generate my WCF client on the fly using code similar to this:

ChannelFactory<IReportBroker> factory = new ChannelFactory<IReportBroker>("ReportBrokerBasicHttpStreamed", new EndpointAddress(this.CurrentSecurityZoneConfigurationManager.ConfigurationSettings[Constants.ConfigurationKeys.ReportBrokerServiceUrl]));
IReportBroker proxy = factory.CreateChannel();
proxy.Execute(requestMessage))

How do I adapt that example to shim the proxy returned back by the CreateChannel method? I am assuming that in the ShimWCFService class, I need to add something like....

ShimChannelFactory<TService>.AllInstances.CreateChannel = (var1) => { return [instance of a mock object]};

However, I am unsure of how to associate a mock object of <TService> with that shim as the return value.


Solution

  • You need to shim the factory for every type parameter. Assume you have the three Service contracts 'IService0' 'IService1' and 'IService2'.

    Then you need to setup the shims like this:

    ShimChannelFactory<IService0>.AllInstances.CreateChannel = (_) => { return new Service0Mock(); }
    ShimChannelFactory<IService1>.AllInstances.CreateChannel = (_) => { return new Service1Mock(); }
    ShimChannelFactory<IService2>.AllInstances.CreateChannel = (_) => { return new Service2Mock(); }