您如何断言Rhino Mocks调用了通用方法? [英] How do you assert a generic method was called with Rhino Mocks?

查看:92
本文介绍了您如何断言Rhino Mocks调用了通用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了以下测试,以验证我的存储库正在调用其各自的会话(我将其重写以突出实际问题):

I have the following test to verify that my repository is calling it's respective session (I've rewritten it to highlight the actual problem):

[Test]
    public void Why_Does_This_Fail()
    {
        var objectUnderTest = new SomeGenericsProblem();

        var fakeSession = MockRepository.GenerateMock<ISession>();
        fakeSession.Expect(s => s.Query<SomeClass>());

        objectUnderTest.NotWorking<SomeClass>();

        fakeSession.AssertWasCalled(t => t.Query<SomeClass>());
    }

但是当我运行测试时,我得到了:

but when I run the test I get this:

System.InvalidOperationException: 通话无效,上次通话已 已使用或未打过电话(进行 确保您正在呼叫虚拟 (C#)/可重写(VB)方法.(C#)/可重写(VB)方法.

System.InvalidOperationException : Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method).(C#) / Overridable (VB) method).

有什么想法我在这里做错了吗?我要嘲笑的会话是一个接口,因此它必须是虚拟的/可覆盖的.

Any ideas what I'm doing wrong here? The session that I'm mocking is an interface, so it has to be virtual/overridable.

我觉得这与我的Query方法是通用的有关,但是我不知道有什么其他方式可以表达我要测试的内容.

I have a feeling it has something to do with the fact that my Query method is a generic, but I don't know any other way to express what I'm trying to test.

此外,如果我删除设置期望值的部分(即此行代码:)

Also, If I remove the part that sets up the expectation (i.e. this line of code:)

fakeSession.Expect(s => s.Query<SomeClass>());

我得到了另一个同样使我感到困惑的异常:

I get a different exception which is equally confusing to me:

System.InvalidOperationException:否 期望被设置为 验证后,确保方法调用 在动作中是虚拟的(C#)/ 可重写(VB.Net)方法调用可重写(VB.Net)方法调用

System.InvalidOperationException : No expectations were setup to be verified, ensure that the method call in the action is a virtual (C#) / overridable (VB.Net) method calloverridable (VB.Net) method call

推荐答案

所以我找出了问题所在.

So I figured out what was wrong.

ISession来自NHibernate,我可能应该提到它.

ISession comes from NHibernate, which I probably should have mentioned.

之所以如此重要,是因为

The reason why this is cruicialy important is because

session.Query<T> 

(这是我要模拟的),是一种扩展方法.

(which is what I'm trying to mock), is an EXTENSION METHOD.

Rhino Mocks显然具有模拟扩展方法的功能,因此为什么它给了我一个奇怪的错误.

Rhino Mocks apparently does not have the capability of mocking extension methods, hence why it's giving me the weird error.

因此,希望我可以节省别人的时间和痛苦,以帮助我弄清楚为什么我的考试不及格.

So hopefully I'll have saves someone else the time and agony I've gone through in trying to figure out why my test won't pass.

为此,我读过的唯一解决方案是实际上更改扩展方法的设计(因为它是NHibernate的一部分,所以无法执行),或者使用诸如TypeMock的其他模拟框架.

The only solution that I've read about for this is to actually change the design of the extension method (which I can't do because it's part of NHibernate), or to use a different mocking framework like TypeMock.

这篇关于您如何断言Rhino Mocks调用了通用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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