Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've been verifying calls in the same manner - I believe it is the right way to do it.</p> <pre><code>mockSomething.Verify(ms =&gt; ms.Method( It.IsAny&lt;int&gt;(), It.Is&lt;MyObject&gt;(mo =&gt; mo.Id == 5 &amp;&amp; mo.description == "test") ), Times.Once()); </code></pre> <p>If your lambda expression becomes unwieldy, you could create a function that takes <code>MyObject</code> as input and outputs <code>true</code>/<code>false</code>...</p> <pre><code>mockSomething.Verify(ms =&gt; ms.Method( It.IsAny&lt;int&gt;(), It.Is&lt;MyObject&gt;(mo =&gt; MyObjectFunc(mo)) ), Times.Once()); private bool MyObjectFunc(MyObject myObject) { return myObject.Id == 5 &amp;&amp; myObject.description == "test"; } </code></pre> <p>Also, be aware of a bug with Mock where the error message states that the method was called multiple times when it wasn't called at all. They might have fixed it by now - but if you see that message you might consider verifying that the method was actually called.</p> <p>EDIT: Here is an example of calling verify multiple times for those scenarios where you want to verify that you call a function for each object in a list (for example).</p> <pre><code>foreach (var item in myList) mockRepository.Verify(mr =&gt; mr.Update( It.Is&lt;MyObject&gt;(i =&gt; i.Id == item.Id &amp;&amp; i.LastUpdated == item.LastUpdated), Times.Once()); </code></pre> <p>Same approach for setup...</p> <pre><code>foreach (var item in myList) { var stuff = ... // some result specific to the item this.mockRepository .Setup(mr =&gt; mr.GetStuff(item.itemId)) .Returns(stuff); } </code></pre> <p>So each time GetStuff is called for that itemId, it will return stuff specific to that item. Alternatively, you could use a function that takes itemId as input and returns stuff.</p> <pre><code>this.mockRepository .Setup(mr =&gt; mr.GetStuff(It.IsAny&lt;int&gt;())) .Returns((int id) =&gt; SomeFunctionThatReturnsStuff(id)); </code></pre> <p>One other method I saw on a blog some time back (Phil Haack perhaps?) had setup returning from some kind of dequeue object - each time the function was called it would pull an item from a queue.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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