title: Manipulating objects with DbSet<T> and IQueryable<T> with NSubstitute returns error
body: <p>I'd like to use <a href="http://nsubstitute.github.io/" rel="nofollow">NSubstitute</a> to unit test Entity Framework 6.x by mocking <a href="http://msdn.microsoft.com/en-us/library/gg696460(v=vs.113).aspx" rel="nofollow">DbSet</a>. Fortunately, <a href="https://github.com/scott-xu" rel="nofollow">Scott Xu</a> provides a good unit testing library, <a href="https://www.nuget.org/packages/EntityFrameworkTesting.Moq/" rel="nofollow">EntityFramework.Testing.Moq</a> using <a href="http://www.moqthis.com/" rel="nofollow">Moq</a>. So, I modified his code to be suitable for NSubstitute and it's been looking good so far, until I wanted to test <code>DbSet&lt;T&gt;.Add()</code>, <code>DbSet&lt;T&gt;.Remove()</code> methods. Here's my code bits:</p>
  
  <pre><code>public static class NSubstituteDbSetExtensions
  {
    public static DbSet&lt;TEntity&gt; SetupData&lt;TEntity&gt;(this DbSet&lt;TEntity&gt; dbset, ICollection&lt;TEntity&gt; data = null, Func&lt;object[], TEntity&gt; find = null) where TEntity : class
    {
      data = data ?? new List&lt;TEntity&gt;();
      find = find ?? (o =&gt; null);
  
      var query = new InMemoryAsyncQueryable&lt;TEntity&gt;(data.AsQueryable());
  
      ((IQueryable&lt;TEntity&gt;)dbset).Provider.Returns(query.Provider);
      ((IQueryable&lt;TEntity&gt;)dbset).Expression.Returns(query.Expression);
      ((IQueryable&lt;TEntity&gt;)dbset).ElementType.Returns(query.ElementType);
      ((IQueryable&lt;TEntity&gt;)dbset).GetEnumerator().Returns(query.GetEnumerator());
  
  #if !NET40
      ((IDbAsyncEnumerable&lt;TEntity&gt;)dbset).GetAsyncEnumerator().Returns(new InMemoryDbAsyncEnumerator&lt;TEntity&gt;(query.GetEnumerator()));
      ((IQueryable&lt;TEntity&gt;)dbset).Provider.Returns(query.Provider);
  #endif
  
      ...
  
      dbset.Remove(Arg.Do&lt;TEntity&gt;(entity =&gt;
                                   {
                                     data.Remove(entity);
                                     dbset.SetupData(data, find);
                                   }));
  
      ...
  
      dbset.Add(Arg.Do&lt;TEntity&gt;(entity =&gt;
                                {
                                  data.Add(entity);
                                  dbset.SetupData(data, find);
                                });
  
      ...
  
      return dbset;
    }
  }
  </code></pre>
  
  <p>And I created a test method like:</p>
  
  <pre><code>[TestClass]
  public class ManipulationTests
  {
    [TestMethod]
    public void Can_remove_set()
    {
      var blog = new Blog();
      var data = new List&lt;Blog&gt; { blog };
  
      var set = Substitute.For&lt;DbSet&lt;Blog&gt;, IQueryable&lt;Blog&gt;, IDbAsyncEnumerable&lt;Blog&gt;&gt;()
                          .SetupData(data);
  
      set.Remove(blog);
  
      var result = set.ToList();
  
      Assert.AreEqual(0, result.Count);
    }
  }
  
  public class Blog
  {
     ...
  }
  </code></pre>
  
  <p>The issue arises when the test method calls <code>set.Remove(blog)</code>. It throws an <code>InvalidOperationException</code> with error message of</p>
  
  <blockquote>
    <p>Collection was modified; enumeration operation may not execute.</p>
  </blockquote>
  
  <p>This is because the fake <code>data</code> object has been modified when the <code>set.Remove(blog)</code> method is called. However, the original Scott's way using <code>Moq</code> doesn't result in the issue.</p>
  
  <p>Therefore, I wrapped the <code>set.Remove(blog)</code> method with a <code>try ... catch (InvalidOperationException ex)</code> block and let the <code>catch</code> block do nothing, then the test doesn't throw an exception (of course) and does get passed as expected.</p>
  
  <p>I know this is not the solution, but how can I achieve my goal to unit test <code>DbSet&lt;T&gt;.Add()</code> and <code>DbSet&lt;T&gt;.Remove()</code> methods?</p>