title: Mocking DbContext - can't insert new objects
body: <p><a href="https://msdn.microsoft.com/en-us/data/dn314429.aspx" rel="nofollow">https://msdn.microsoft.com/en-us/data/dn314429.aspx</a></p>
  
  <p>I'm trying to use code similar to query scenario from link above.
  EF 6.1 + Moq.
  It works fine with pre-populated data like this (I can query them without any problem):</p>
  
  <pre><code>_context = new Mock&lt;MyContext&gt;();
  
  IQueryable&lt;users&gt; users = new List&lt;users&gt;
          {
              new users{id = 1, email = "test@test.pl", password = "test", created = DateTime.Now, modified = DateTime.Now},
              new users{id = 2, email = "test2@test.pl", password = "test", created = DateTime.Now, modified = DateTime.Now},
          }.AsQueryable();
  var mockUsers = new Mock&lt;DbSet&lt;users&gt;&gt;();
          mockUsers.As&lt;IQueryable&lt;users&gt;&gt;().Setup(m =&gt; m.Provider).Returns(users.Provider);
          mockUsers.As&lt;IQueryable&lt;users&gt;&gt;().Setup(m =&gt; m.Expression).Returns(users.Expression);
          mockUsers.As&lt;IQueryable&lt;users&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(users.ElementType);
          mockUsers.As&lt;IQueryable&lt;users&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(users.GetEnumerator());
  
  _context.Setup(m =&gt; m.users).Returns(mockUsers.Object);
  </code></pre>
  
  <p>But when I try to add new object in my test method, it doesn't work:</p>
  
  <pre><code>[TestMethod]
      public void AddUser()
      {
          _context.Object.users.Add(new users { email = "test3@test.pl", password = "test", created = DateTime.Now, modified = DateTime.Now });
          _context.Object.SaveChanges();
          var count = _context.Object.users.Count();
          Assert.AreEqual(3, count); // count == 2 instead 3
      }
  </code></pre>
  
  <p>Would it be possible to modify it, so my mocked context would actually apply SaveChanges? Am I thinking correctly or this approach would be wrong for some reason?</p>
  
  <p>If i use non-query approach I won't be able to use:</p>
  
  <pre><code>mockContext.Object.users.Count()
  </code></pre>
  
  <p>Let's say I have method to test, with fragment like this:</p>
  
  <pre><code> context.cards.Add(card);
   context.SaveChanges();
   SetCardForUser(card.id, user);
  
   protected void SetCardForUser(long cardId, users user)
      {
          user.card_id = cardId;
          context.SaveChanges();
      }
  </code></pre>
  
  <p>I need card.id returned after inserting object to database.
  Should I keep strictly to SRP and have methods that will simply do only one thing?</p>
  
  <p>How would I test this method where only AddCardAndStartAuthCharge is public and other methods are protected? :</p>
  
  <pre><code>public async Task&lt;long&gt; AddCardAndStartAuthCharge(CardModel model, string username)
      {
          var hash = await GetHashForCardAuth(model);
          var CardWithId = AddCardToDatabase(model, username);
          long chargeId = AddHashToCard(CardWithId, hash);
          return chargeId;
      }
  </code></pre>