Note that there are some explanatory texts on larger screens.

plurals
  1. POMocked object accessed an unknown number of times by Linq To Sql statement
    text
    copied!<p>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:</p> <pre><code>return SessionService.Items.OrderBy(x =&gt; x.Id) .FirstOrDefault(x =&gt; x.Required &amp;&amp; !x.Complete); </code></pre> <p>I'm my unit test, I am mocking the service to return a list of items:</p> <pre><code>var mocks = new MockRepository(); var mockSessionService = mocks.StrictMock&lt;ISessionService&gt;(); using (mocks.Record()) { Expect.Call(mockSessionService.Items).IgnoreArguments() .Return(MockItems); } ObjectFactory.Inject(typeof(ISessionService), mockSessionService); var inventoryService = ObjectFactory.GetInstance&lt;IInventorySurveyService&gt;(); var item = inventoryService.GetNextSurveyItem(); Assert.IsNotNull(item); Assert.That(item.Id == Guid.Parse("26fd9364-d831-477a-9faf-25271562f144")); </code></pre> <p>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:</p> <pre><code>Expect.Call(mockSessionService.Items).IgnoreArguments() .Return(MockItems).Repeat.Any(); </code></pre> <p>Is this right? How many times is my list being referenced?</p> <p>Edit: added more of the code sample</p> <p><strong>More Editing</strong>:</p> <p>It seems like we might be misunderstanding each other. I understand the code you posted is to verify with my unit test that the call to my SessionService only occurs once. </p> <p>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?)</p> <p>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:</p> <pre><code>return SessionService.Items.OrderBy(x =&gt; x.Id) .FirstOrDefault(x =&gt; x.Required &amp;&amp; !x.Complete); </code></pre> <p>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!)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload