【问题标题】:Rhino mocks is unable to set an expectation on the 2nd call of mocked object whose interface returns a Func<T>Rhino 模拟无法对接口返回 Func<T> 的模拟对象的第二次调用设置期望
【发布时间】:2014-02-04 20:29:35
【问题描述】:

我试图在一个模拟接口上设置一个期望,该接口从它的一个方法返回一个函数。不幸的是,在投射第二个返回类型时,它看起来像 rhino 模拟窒息,或者至少这是我从异常中得到的。异常是从 Expect 调用中引发的。

public interface IFuncTest
{
    Func<int> Generate(int num);
}

[Test]
public void MyTest()
{
    var myMock = MockRepository.GenerateStrictMock<IFuncTest>();

    // first expectation will succeed
    myMock.Expect(mm => mm.Generate(42)).Return(() => 6)

    // second expectation will throw inside the lambda passed to Expect
    myMock.Expect(mm => mm.Generate(4000)).Return(() => 9)
}

期望的顺序无关紧要。

异常文本:

Unable to cast object of type 'Castle.Proxies.ProxyDelegate_Func`1_1Proxy5f00ec19448d4e4c96f5d6df12f2c87d' to type 'System.Func`1[System.Int32]'.
    at Castle.Proxies.IFuncTest`1Proxy2cc837d117f745dfb24033b9bf7cb0f4.IFuncTest`1.Generate(Int32 tee)
    at Tests.Tests.<MyTest>b__9(IFuncTest`1 mm) in Tests\Tests.cs:line 167
    at Rhino.Mocks.RhinoMocksExtensions.Expect[T,R](T mock, Function`2 action)
    at Tests.Tests.MyTest() in Tests\Tests.cs:line 167

关于为什么会出现这种情况或如何解决它的任何想法?

【问题讨论】:

标签: c# unit-testing mocking rhino-mocks


【解决方案1】:

我最近提交了一个与 Action 类似的错误。

见:https://github.com/ayende/rhino-mocks/issues/21#event-707223576

它已经修复了。此修复程序还修复了此问题。如果您从 github 获取源代码并构建它就可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多