title: Moq is slow to verify dependency after a large number calls
body: <p>I've hit a snag when using Moq to simulate an dependency which is called a large number of times. When I call <code>Verify</code>, Moq takes a long time (several minutes) to respond, and sometimes crashes with a <code>NullReferenceException</code> (I guess this is understandable, given the amount of data that <code>Moq</code> would have to accumulate to do the <code>Verify</code> from a "cold start").</p>
  
  <p>So my question is, is there another strategy that I can use to do this using Moq, or should I revert to a hand-crafted stub for this rather unusual case. Specifically, is there a way to tell Moq up front that I'm only interested in verifying <em>specific filters</em> on the parameters, and to ignore all other values?</p>
  
  <p>Neither of the approaches below is satisfactory.</p>
  
  <p><strong>Given CUT and Dep</strong>:</p>
  
  <pre><code>public interface ISomeInterface
  {
      void SomeMethod(int someValue);
  }
  
  public class ClassUnderTest
  {
      private readonly ISomeInterface _dep;
      public ClassUnderTest(ISomeInterface dep)
      {
          _dep = dep;
      }
  
      public void DoWork()
      {
          for (var i = 0; i &lt; 1000000; i++) // Large number of calls to dep
          {
              _dep.SomeMethod(i);
          }
      }
  }
  </code></pre>
  
  <p><strong>Moq Strategy 1 - Verify</strong></p>
  
  <pre><code>        var mockSF = new Mock&lt;ISomeInterface&gt;();
          var cut = new ClassUnderTest(mockSF.Object);
          cut.DoWork();
          mockSF.Verify(mockInt =&gt; mockInt.SomeMethod(It.Is&lt;int&gt;(i =&gt; i == 12345)),
                        Times.Once());
          mockSF.Verify(mockInt =&gt; mockInt.SomeMethod(It.Is&lt;int&gt;(i =&gt; i == -1)),
                        Times.Never());
  </code></pre>
  
  <p><strong>Moq Strategy 2 - Callback</strong></p>
  
  <pre><code>        var mockSF = new Mock&lt;ISomeInterface&gt;();
          var cut = new ClassUnderTest(mockSF.Object);
          bool isGoodValueAlreadyUsed = false;
          mockSF.Setup(mockInt =&gt; mockInt.SomeMethod(It.Is&lt;int&gt;(i =&gt; i == 12345)))
                .Callback(() =&gt;
              {
                  if (isGoodValueAlreadyUsed)
                  {
                      throw new InvalidOperationException();
                  }
                  isGoodValueAlreadyUsed = true;
              });
          mockSF.Setup(mockInt =&gt; mockInt.SomeMethod(It.Is&lt;int&gt;(i =&gt; i == -1)))
                .Callback(() =&gt;
                  { throw new InvalidOperationException(); });
  
          cut.DoWork();
          Assert.IsTrue(isGoodValueAlreadyUsed);
  </code></pre>