title: How to setup Moq for the same method where return value depends on input?
body: <p>I'd like to test a method called multiple times during test, in a way where the tested method output depends on its input. I checked Moq's documentation and the following example seems to what I need.</p>
  
  <pre><code>// matching Func&lt;int&gt;, lazy evaluated
  mock.Setup(foo =&gt; foo.Add(It.Is&lt;int&gt;(i =&gt; i % 2 == 0))).Returns(true);
  </code></pre>
  
  <p>The tested method looks like this:</p>
  
  <pre><code>Task&lt;DimensionStructure&gt; GetDimensionStructureByIdAsync(DimensionStructure dimensionStructure);
  </code></pre>
  
  <p>The DimensionStructure class looks like this, it is simplified as on the <code>Id</code> is important.</p>
  
  <pre><code>public class DimensionStructure
      {
          public long Id { get; set; }
      }
  </code></pre>
  
  <p>But when I put together the mock I need, example below, when it is called it returns always <code>null</code>.</p>
  
  <pre><code>_masterDataWebApiClientMock.Setup(m =&gt; m.GetDimensionStructureByIdAsync(
                      It.Is&lt;DimensionStructure&gt;(
                          structure =&gt; structure.Id == 101)
                  ))
                 .ReturnsAsync(addedToRoot);
  _masterDataWebApiClientMock.Setup(m =&gt; m.GetDimensionStructureByIdAsync(
                      It.Is&lt;DimensionStructure&gt;(
                          structure =&gt; structure.Id == 201 )
                  ))
                 .ReturnsAsync(addedToFirstLevel);
  </code></pre>
  
  <p>The point os that the method gets different parameter and returns different objects.
  In both cases, it returns <code>null</code>. According to the doc, it should return what is in the <code>ReturnAsync</code> which is not null.</p>
  
  <p>It works perfectly in another test where the <code>Id</code> property value of the object doesn't matter.</p>
  
  <pre><code>_masterDataWebApiClientMock.Setup(m =&gt; m.GetDimensionStructureByIdAsync(It.IsAny&lt;DimensionStructure&gt;()))
                 .ReturnsAsync(addedDimensionStructure);
  </code></pre>
  
  <p>The question is, how to solve this?</p>
  
  <p><strong>Update - 02/29/2020</strong></p>
  
  <p>When I do not specify the call parameter then it is just working fine. It makes a bit tricky to test the method, but solvable.</p>
  
  <pre><code>_masterDataWebApiClientMock
                 .Setup(m =&gt; m.GetDimensionStructureByIdAsync(
                      It.IsAny&lt;DimensionStructure&gt;()
                  ))
                 .ReturnsAsync(addedToRoot);
  </code></pre>
  
  <p>On the other hand I <a href="https://github.com/moq/moq4/issues/973" rel="nofollow noreferrer">reported</a> this case a special, possible bug case to Moq folks.</p>