2011-07-13 53 views
1
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 

2类PagingInfo是2个实例,但具有相同的值,并在assert之前进行验证。RhinoMocks异常没有意义

这里是单元测试代码

 //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()); 
     } 
+7

请显示验证码 –

回答

1

问题就迎刃而解了:

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()?因为有时你需要嘲笑大对象,也许你只想测试一小部分。 我通常使用它,当我有对象作为参数。 避免使用它的另一种方法是对两个对象使用相同的散列码,这两个对象用于记录,另一个用作播放中的参数。

2

作为丹尼尔指出的,共享一些代码。

我最好的猜测是:你已经创建了一个严格的模拟,并且你的测试正在导致预期不出现的模拟(“实际#1”)发生了什么(“预期#0”)。在严格模拟中没有任何事情可以发生,这在安排阶段没有明确配置。

+0

我该如何对前面显示的上下文进行部分模拟或动态模拟? –