title: Verifying an enumerable list in Moq
body: <p>I'm trying to write a unit test for a method which looks like this:</p>
  
  <pre><code>public int Save(IEnumerable&lt;int&gt; addedIds, IEnumerable&lt;int&gt; removedIds)
  {
      var existingIds = repository.Get();
      IEnumerable&lt;int&gt; ids = existingIds.Except(removedIds).Union(addedIds));
      return repository.Create(ids);
  }
  </code></pre>
  
  <p>The test in Moq looks like this:</p>
  
  <pre><code>repository.Setup(r =&gt; r.Get()).Returns(CreateList());
  service.Save(addedIds, removedIds);
  repository.Verify(r =&gt; r.Create(It.Is&lt;IEnumerable&lt;int&gt;&gt;(l =&gt; VerifyList(l))));
  </code></pre>
  
  <p>This fails, with this error<s>, and <code>VerifyList()</code> is never called:</s></p>
  
  <blockquote>
    <p>Expected invocation on the mock at least once, but was never
    performed:</p>
    
    <p><code>r =&gt; r.Create(It.Is&lt;IEnumerable'1&gt;(list =&gt; VerifyList(list)))</code></p>
    
    <p>Performed invocations:</p>
    
    <p><code>IRepo.Create(System.Linq.Enumerable+&lt;UnionIterator&gt;d__88'1[System.Int32])</code></p>
  </blockquote>
  
  <p>As the invoked type is not <code>IEnumerable&lt;int&gt;</code> but is in fact <code>System.Linq.Enumerable+&lt;UnionIterator&gt;d__88'1[System.Int32])</code>, the test fails. (Stepping through the test, everything is happening correctly and the results are as expected)</p>
  
  <p>If I call <code>ids.ToList()</code> in the method under test, these are the results:</p>
  
  <blockquote>
    <p>Expected invocation on the mock at least once, but was never performed:</p>
    
    <p><code>r =&gt; r.Create(It.Is&lt;List'1&gt;(l =&gt; VerifyList(l)))</code></p>
    
    <p>Performed invocations:
    <code>IRepo.Create(System.Collections.Generic.List'1[System.Int32])</code></p>
  </blockquote>
  
  <p>Is there any way round this? Or am I doing something wrong?</p>
  
  <p>Edit: it turns out I had a mistake in my VerifyList method so it was returning false, but Moq wasn't giving me that information. The type difference is a red herring..</p>