title: Moq Setup: ability to pass a value or It.IsAny<T>() depending on condition
body: <p>[The question is similar to this: <a href="https://stackoverflow.com/questions/17053064/what-is-the-difference-between-passing-it-isanyint-and-the-value-of-it-isany">What is the difference between passing It.IsAny&lt;int&gt;() and the value of It.IsAny&lt;int&gt;() to a method setup</a> - needing additional clarification]</p>
  
  <h2>Short version</h2>
  
  <p>(More definitions below)</p>
  
  <p>I am in a situation where <strong>I need to setup mocks in a factory</strong>. Depending on the value, I want to <strong>choose between</strong> using it or <code>It.IsAny&lt;TTypeOfValue&gt;()</code>. </p>
  
  <p>This is what I (naively) wanted to do:</p>
  
  <pre><code>moq.Setup(() =&gt; mockWebService.WebServiceMethod(
      webServiceMethodObject.Some ?? It.IsAny&lt;string&gt;(), // that fails
      ...,
      out webServiceMethodObject.That,
      ...)).Returns(webServiceMethodObject.ReturnEnum);
  </code></pre>
  
  <p>I have such large parameterlists (old legacy webservice methods) and so much different combination of values that I don't want to write it by hand, nor using <code>It.IsAny&lt;T&gt;()</code> everywhere, as I want to control the return value depending on parameters.</p>
  
  <p>A possible alternative should be the version, where I could match concrete values with concrete return types, and when no concrete values can be matched, it falls back to the broadest version (with all parameters substituted with <code>It.IsAny&lt;T&gt;()</code>. (Short example: in a login test I'd like to test different return values of different input parameters of a login method. In all other tests I'd just like to return LoginSuccess).</p>
  
  <p>How can I achieve this? Only with some Expression/Reflection magic?</p>
  
  <hr>
  
  <h2>Longer explanation</h2>
  
  <p>This is how our typical legacy webservice looks like:</p>
  
  <pre><code>ReturnValueEnum WebServiceMethod(string some, int thing, ..., out int that, out byte[] those, ...) { ... }
  </code></pre>
  
  <p>We had so many webservice method calls needed and they are so bloated with parameters, that we had to come up with encapsulating them in objects.
  Example:</p>
  
  <pre><code>public class WebServiceMethodObject
  {
      public string Some { get; set; }
      public int Thing { get; set; }
      ...
      public ReturnValue ReturnEnum { get; set; }
  }
  </code></pre>
  
  <p><strong>The strategy should be this:</strong> we are creating a default version of this object. In the tests we fill up values that needed to be matched. We would like to pass the object to a to-be-written method, which sets up the mock accordingly: </p>
  
  <p><strong>if a property is set:</strong> use that value</p>
  
  <p><strong>else:</strong> use <code>It.IsAny&lt;T&gt;()</code> (see the Setup above!).</p>