RhinoMocks的 - 惩戒代表 [英] Rhinomocks - Mocking delegates

查看:106
本文介绍了RhinoMocks的 - 惩戒代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 ?

推荐答案

刚刚找到了解决办法。这似乎是有点难看,但它是第一次迭代只可能更优雅版很快就会出现。我们的想法是创建另一个存根和匹配 Func键<>反对

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 +扩展名被用作测试框架。请忽略 InlineData 的东西 - 它只是另一种方式来摆脱不必要的测试设置的。

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.

下面是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 :)

这篇关于RhinoMocks的 - 惩戒代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆