Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhinomocks - Mocking delegates

public interface IServiceInvoker
{  
    R InvokeService<T, R>(Func<T, R> invokeHandler) where T : class;
}

public class MediaController : Controller
{ 
    private IServiceInvoker _serviceInvoker;
    public MediaController(IServiceInvoker serviceInvoker)
    {
        _serviceInvoker = serviceInvoker;
    }

    public JsonResult GetAllMedia()
    {
        var media = _serviceInvoker.InvokeService<IMediaService, List<MediaBase>>(proxy    => proxy.GetAllMediaInJson());

        JsonResult jsonResult = new JsonResult();
        jsonResult.Data = media;
        jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
        return jsonResult;

}


[TestClass]
public class MediaControllerTests
{
    [TestMethod]
    public void GetAllMedia()
    {
        JsonResult data;
        var serviceInvoker = MockRepository.GenerateStub<IServiceInvoker>();
        var media = CreateSeveralMedia();
        serviceInvoker.Stub(c => c.InvokeService<IMediaService, List<MediaBase>>(p => p.GetAllMediaInJson())).Return(media);
        data = new MediaController(serviceInvoker).GetAllMedia();
        serviceInvoker.VerifyAllExpectations();
        Assert.IsNotNull(data);
    }

}

I am stubbing the service and returning a collection. When I run this test, media is null. Any idea, how can I set expectations on this mock ?

like image 213
Abhilash Avatar asked Nov 26 '25 09:11

Abhilash


1 Answers

Just found a solution. It seems to be a little ugly, but it is the first iteration only probably more elegant version will appear soon. The idea is to create another stub and match Func<> against it: I will provide code for my use case:

[Theory]
[InlineData(342, 31129, 3456)]
public void should_call_service_invoker_and_return_result(int number1, int number2, int expected)
{
    var calculator = MockRepository.GenerateStub<ICalculator>();
    calculator.Stub(_ => _.Add(number1, number2)).Return(expected);
    var serviceInvoker = MockRepository.GenerateStub<ServiceInvoker<ICalculator>>();
    serviceInvoker
        .Stub(_ => _.Invoke(Arg<Func<ICalculator, int>>.Matches(d => d(calculator) == calculator.Add(number1, number2))))
        .Return(expected);
    var serviceConsumer = new ServiceConsumer(serviceInvoker);

    var actual = serviceConsumer.GetAddResultFor(number1, number2);

    Assert.Equal(expected, actual);
}

xUnit + extensions is used as testing framework. Please ignore Theory and InlineData stuff -- it is just another way to get rid of unnecessary test setup.

Here is the code of SUT:

public class ServiceConsumer
{
    private readonly ServiceInvoker<ICalculator> serviceInvoker;

    public ServiceConsumer(ServiceInvoker<ICalculator> serviceInvoker)
    {
        this.serviceInvoker = serviceInvoker;
    }

    public int GetAddResultFor(int number1, int number2)
    {
        return serviceInvoker.Invoke(_ => _.Add(number1, number2));
    }
}

public class ServiceInvoker<T>
{
    public virtual R Invoke<R>(Func<T, R> func)
    {
        throw new NotImplementedException();
    }
}

public interface ICalculator
{
    int Add(int number1, int number2);
}

Hope this will be helpful. Any suggestions of how to add more beauty are welcome :)

like image 68
the_joric Avatar answered Nov 28 '25 22:11

the_joric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!