开发者

Mocked object accessed an unknown number of times by Linq To Sql statement

开发者 https://www.devze.com 2023-03-10 10:27 出处:网络
I\'m writing unit tests for a method that uses an object I am holding as a session variable accessed by a service I called \"SessionService\". The full method is as follows:

I'm writing unit tests for a method that uses an object I am holding as a session variable accessed by a service I called "SessionService". The full method is as follows:

return SessionService.Items.OrderBy(x => x.Id)
    .FirstOrDefault(x => x.Required && !x.Complete);

I'm my unit test, I am mocking the service to return a list of items:

var mocks = new MockRepository();

var mockSessionService = mocks.StrictMock<ISessionService>();
using (mocks.Record())
{
    Expect.Call(mockSessionService.Items).IgnoreArguments()
        .Return(MockItems);
}
ObjectFactory.Inject(typeof(ISessionService), mockSessionService);

var inventoryService = 
    ObjectFactory.GetInstance<IInventorySurveyService>();

var item = inventoryService.GetNextSurveyItem();

Assert.IsNotNull(item);
Assert.That(item.Id == Guid.Parse("26fd9364-d831-477a-9faf-25271562f144"));

What I'm finding is that my linq statement seems to be referencing my "Items" list more than once because my unit tests throw an error unless I append ".Repeat.Any()" to my "Expect" statement:

Expect.Call(mockSessionService.Items).IgnoreArguments()
    .Return(MockItems).Repeat.Any();

Is this right? How many times is my list being referenced?

Edit: added more of the code sample

More Editing:

It seems like we might be misunderstanding each other. I understand the code you posted is to verify with my 开发者_Python百科unit test that the call to my SessionService only occurs once.

What I am using my "Expect" call for is to mock up that call to the SessionService and return an expected response, and what my test is actually looking to verify is what is in the Assert statements at the bottom of my test. (I think my use of this is incorrect- I should be using a stub instead of a mock?)

What I was asking in my original question didn't have anything to do with my unit test's results, I was merely wondering how many times the SessionService is called in this statement when I executed my unit test:

return SessionService.Items.OrderBy(x => x.Id)
    .FirstOrDefault(x => x.Required && !x.Complete);

I just want the answer, I don't need to know how to test for the answer (though I do know how to do that now- thanks!)


First of all, calling the IgnoreArguments method is not necessary in this case because you're performing a property mock, which never will ask for arguments.

Then, the use of Repeat.Any hides you the real problem. Is a good practice to test how many times your method should be call. If you expect only one time I suggest you to explicity put the Repeat.Once() clause.

Ok, this line show me that you're replacing the real instance with the mock one:

ObjectFactory.Inject(typeof(ISessionService), mockSessionService);

Then you have to stop recording and start verifying your records before and after the method call. Something like this:

mocks.ReplayAll();

var item = inventoryService.GetNextSurveyItem();

mocks.VerifyAll();

Tell me if they solve your problem, thanks for voting.

Regards.

0

精彩评论

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