0

I am trying to figure how to create tests for my controllers that are consuming a WCF service (via a proxy class)

The proxy class is pretty much identical to the one listed in this post http://blog.weminuche.net/2008/08/test-post.html

Base Controller

public abstract class ServiceProxyController<TService> : Controller
    where TService : class 
{

    private readonly ServiceProxy<TService> _proxyHelper;

    protected ServiceProxyController(string endpoint)
    {
        _proxyHelper = new ServiceProxy<TService>(endpoint);
    }

    private Stuff GetStuff(int num)
    {
         Call((service) =>  {
                    service.DoSomeStuff(num)
                 });
                ................
            }
     ...........
 }

Controller Implementation

public class MyController : ServiceProxyController<IService>
{
    public MyController() : base("ServiceBindingName")
    {
    }

     }

I want to be able to inject a proxy helper(???) into my controller so as I can mock it and therefor test the controller

David
  • 8,340
  • 7
  • 49
  • 71

2 Answers2

1

How about injecting the proxy helper to the constructor (notice the introduction of an abstraction):

private readonly IServiceProxy<TService> _proxyHelper;
protected ServiceProxyController(IServiceProxy<TService> proxyHelper)
{
    _proxyHelper = proxyHelper;
}

and the controller:

public MyController(IServiceProxy<TService> proxyHelper) 
    : base(proxyHelper)
{
}

This way in your unit test when instantiating the controller you could inject a mocked instance of the IServiceProxy<TService> interface.

You will then need to configure your DI framework to insert the proper implementation into the controller constructor which will wrap the actual ChannelFactory.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I think I get what your saying but the problem is that the ServiceProxy class' Call method uses the ChannelFactory to create a channel that is used when calling the delegate – David Apr 13 '11 at 15:01
  • @Dve, that's why you need to introduce an abstraction: `IServiceProxy` which will contain the necessary Call method. Then in the real implementation you could use the actual ChannelFactory and in the unit test you will simply mock this method. – Darin Dimitrov Apr 13 '11 at 15:05
  • But the delegate argument to the Call method will still need to be supplied and in turn will require a service to be passed to it – David Apr 13 '11 at 15:24
  • please note [my answer](http://stackoverflow.com/questions/5650804/rhinomocks-mocking-delegates/6672001#6672001) of how you can set expectations for delegates – the_joric Jul 13 '11 at 11:01
0

I just asked a similar question. I am injecting the service using structure map. I am dynamically creating a proxy using channel factory.

Look at this example for using Channel factory.

creating WCF ChannelFactory<T>

My question for your reference.

Rhinomocks - Mocking delegates

Note- Actually it was Darin who posted the ServiceInvoker

Community
  • 1
  • 1
Abhilash
  • 121
  • 1
  • 7