Rhinomocks - Mocking delegates

两盒软妹~` 提交于 2019-12-01 22:08:06

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 :)

The lambda in your unit test compiles into a class-level method (a method inside your unit test). Inside your controller, a different lambda compiles into a class-level method (inside the controller). The stub set up in your unit test doesn't match the stub being executed in your controller, so Rhino Mocks returns a default (null). More here: http://groups.google.com/group/rhinomocks/browse_frm/thread/a33b165c16fc48ee?tvc=1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!