【问题标题】:RhinoMocks exception that has no meaning没有意义的 RhinoMocks 异常
【发布时间】:2011-07-13 15:01:51
【问题描述】:
 Rhino.Mocks.Exceptions.ExpectationViolationException was unhandled by user code
 Message=Service.GetCommunityLightPagered(null, 1, null, null, Data.PagingInfo); 
 Expected #0, Actual #1.
 Service.GetCommunityLightPagered(null, 1, null, null, Data.PagingInfo); 
 Expected #1, Actual #0.
 Source=Rhino.Mocks

PagingInfo 的 2 个类是 2 个实例,但具有相同的值,并且之前使用断言进行了验证。

这是单元测试代码

        //Arrange
        GetController("user1");

        //Act
        using (MockRepository.Record())
        {
            Expect.Call(
                ServiceClient.GetMock<Service>().GetUserPermissionSet(
                    "user1", false, false, false)).Return(
                        Db.User.Permissions.Where(p => p.Name == "CreateCommunity").ToArray());
        }
        using (MockRepository.Playback())
        {
            ActionResult result = Controller.ExecuteAction<int?, int?, string, string, string, string, string, string>(ServiceClient, Controller.SearchCommunities, null, null,null, null, null, CommunityTypeEnum.HighSchool, null,null);
            //Assert
            Assert.AreEqual(typeof(PartialViewResult), result.GetType());
        }

【问题讨论】:

  • 请出示代码

标签: c# unit-testing rhino-mocks


【解决方案1】:

正如丹尼尔所说,分享一些代码。

我的最佳猜测:您创建了一个严格的模拟,并且您的测试导致模拟(“实际 #1”)发生了一些意外(“预期 #0”)。在 Arrange 阶段未明确配置的严格模拟上不会发生任何事情。

【讨论】:

  • 如何在我之前展示的上下文中进行部分模拟或动态模拟?
【解决方案2】:

问题解决了:

Expect.Call(ServiceClient.GetMock<IUserService>().GetCommunityLightPagered(null, 1, null,null, new PagingInfo
                {
                    Page = 1,
                    Rows = 10,
                    SortColumn = "Id",
                    SortOrder = "desc"
                })
                ).IgnoreArguments().Return(TestHelper.CommunityInfoLightDTO());

现在所有对此的调用都将被视为有效。

编辑 1:为什么应该使用 IgnoreArguments()?因为有时你有需要模拟的大对象,也许你只想测试它的一小部分。 当我有对象作为参数时,我通常使用它。 另一种避免使用它的方法是对两个对象使用相同的哈希码,一个用于记录,一个用作回放中的参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 2012-04-02
    相关资源
    最近更新 更多