开发者

RhinoMocks exception that has no meaning

 Rhino.Mocks.Exceptions.ExpectationViolationException was unhandled by user code
 M开发者_JAVA技巧essage=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

The 2 classes PagingInfo are 2 instances but have the same values and are verified before with asserts.

Here is the unit test code

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


As Daniel noted, share some code.

My best guess: you have created a strict mock, and your test is causing something to happen to on the mock ("Actual #1") that was not expected ("Expected #0"). Nothing on can happen on a strict mock that wasn't explicitly configured in the Arrange phase.


problem solved :

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

Now all calls to this ,will be considered valid.

Edit 1: Why you should use IgnoreArguments()? Because sometimes you have big objects that need to be mocked and maybe you only want to test a small part of it. I usually use it when i have objects as parameters. Another way to avoid using it is by using same hashcode to both objects, the one used for record and the one used as param in playback.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜