title: EF 6 fake db context, can't find the entity
body: <p>I'm in the middle of covering some of our service classes with unit tests and I have managed to isolate/fake the dbcontext using NSubstitute (following <a href="https://msdn.microsoft.com/en-us/data/dn314429.aspx?f=255&amp;MSPPError=-2147217396" rel="nofollow">this</a> guide). I have some tests done and working, and things seemed to be alright, but now I can't find an entity I added to the context.</p>
  
  <p>The test code is pretty straightforward:</p>
  
  <pre><code>[Fact]
  public void CreateStore_GivenAccount_AccountIsAssignedTheStore()
  {
      const int accountId = 10;
      var account = new Account {Id = accountId};
      var fakeContext = new FakeContextBuilder()
          .WithAccounts(account)
          .Build();
      var service = new Service(fakeContext);
      const int someProperty = 0;
      const string someOtherProperty = "blabla";
  
      service.CreateStore(accountId, someProperty, someOtherProperty);
  
      var storeWasAdded = account.Stores
          .Any(store =&gt;
              store.SomeProperty == someProperty &amp;&amp;
              store.SomeOtherProperty == someOtherProperty);
      Assert.True(storeWasAdded);
  }
  </code></pre>
  
  <p>The <code>FakeContextBuilder</code> is a helper class I made for setting up the context (similar methods for other entities):</p>
  
  <pre><code>public class FakeContextBuilder
  {
      private DbSet&lt;Account&gt; _accountTable;
  
      private static DbSet&lt;TEntity&gt; SetUpFakeTable&lt;TEntity&gt;(params TEntity[] entities) where TEntity : class
      {
          var fakeTable = Substitute.For&lt;DbSet&lt;TEntity&gt;, IQueryable&lt;TEntity&gt;&gt;() as IQueryable&lt;TEntity&gt;;
          var table = entities.AsQueryable();
          fakeTable.Provider.Returns(table.Provider);
          fakeTable.Expression.Returns(table.Expression);
          fakeTable.ElementType.Returns(table.ElementType);
          fakeTable.GetEnumerator().Returns(table.GetEnumerator());
          return (DbSet&lt;TEntity&gt;) fakeTable;
      }
  
      public Context Build()
      {
          var context = Substitute.For&lt;Context&gt;();
          context.Accounts.Returns(_accountTable);
          return context;
      }
  
      public FakeContextBuilder WithAccounts(params Account[] accounts)
      {
          _accountTable = SetUpFakeTable(accounts);
          return this;
      }
  }
  </code></pre>
  
  <p>Service method:</p>
  
  <pre><code>public void CreateStore(int accountID, int someProperty, string someOtherProperty)
  {
      var account = _context.Accounts.Find(accountID);
      account.Stores.Add(new Store(someProperty, someOtherProperty));
  }
  </code></pre>
  
  <p>On the <code>Accounts.Find()</code> row I get <code>null</code> instead of the expected account instance. If I add a breakpoint and look at the context I see that "enumerate results" on Accounts yields no results, but I can see that the provider and enumerator etc are set correctly in non-public members. The fake context builder also works fine in other tests, so my guess is that this is related to the Find() method.</p>
  
  <p>EDIT: I have now confirmed that the Find() method is the culprit since the test passes when doing this instead:</p>
  
  <pre><code>var account = _context.Accounts.Single(act =&gt; act.Id == 10);
  </code></pre>
  
  <p>I still want to use Find() for caching purposes and so on. Can this be configured in the test code somehow? Would hate to mess up the production code for this, since it's really a simple operation.</p>