title: How to unit test a repository pattern that uses Entity Framework?
body: <p>I'm currently trying to unit test a repository I made through Entity Framework:</p>
  
  <p>What I want to happen is that test the repository without actually sending/connecting to the actual database, I want to this without using any mocking framework.</p>
  
  <p>Currently my test is sending the data to the database, what I want to happen is test the add/remove etc. methods without sending the actual data to the database since it's only for testing.</p>
  
  <p>Here is the repository:</p>
  
  <pre><code>namespace AbstractFactory.Repository
  {
      using System.Collections.Generic;
      using System.Data.Entity;
      using System.Linq;
  
      /// &lt;summary&gt;
      /// This class serves as the structure of the Author repository using a database
      /// &lt;/summary&gt;
      public class DbAuthorRepository : IRepository&lt;AuthorEntity&gt;
      {
  
          private AbstractFactoryPatternEntities context;
  
          public DbAuthorRepository(AbstractFactoryPatternEntities context)
          {
              this.context = context;
          }
  
          /// &lt;summary&gt;
          /// Adds the specified author.
          /// &lt;/summary&gt;
          /// &lt;param name="author"&gt;The author.&lt;/param&gt;
          public void Add(AuthorEntity author)
          {
              context.AuthorEntities.Add(author);
          }
  
          /// &lt;summary&gt;
          /// Removes the specified author.
          /// &lt;/summary&gt;
          /// &lt;param name="author"&gt;The author.&lt;/param&gt;
          public void Remove(AuthorEntity author)
          {
              this.context.AuthorEntities.Remove(author);
          }
  
          /// &lt;summary&gt;
          /// Saves this instance.
          /// &lt;/summary&gt;
          public void Save()
          {
              this.context.SaveChanges();
          }
  
          /// &lt;summary&gt;
          /// Gets all.
          /// &lt;/summary&gt;
          /// &lt;returns&gt;returns a list of all the authors&lt;/returns&gt;
          public IEnumerable&lt;AuthorEntity&gt; GetAll()
          {
              List&lt;AuthorEntity&gt; result = this.context.AuthorEntities.Include(a =&gt; a.Books).ToList();
  
              return result;
          }
  
          /// &lt;summary&gt;
          /// Gets the author by id.
          /// &lt;/summary&gt;
          /// &lt;param name="id"&gt;The id.&lt;/param&gt;
          /// &lt;returns&gt;returns an entity&lt;/returns&gt;
          public AuthorEntity GetById(int id)
          {
              AuthorEntity result = this.context.AuthorEntities.Single(a =&gt; a.AuthorId == id);
  
              return result;
          }
      }
  }
  </code></pre>
  
  <p>Here is the current code for the unit test:</p>
  
  <pre><code>[TestMethod]
          public void Add_MethodIsCalled_EntityCountIsIncrementedByOne()
          {
              using (ShimsContext.Create())
              {
                  ShimAbstractFactoryPatternEntities context = new ShimAbstractFactoryPatternEntities(new AbstractFactoryPatternEntities());
                  DbAuthorRepository repository = new DbAuthorRepository(context);
                  repository.Add(new AuthorEntity { FirstName = "Test", LastName = "testing=" });
                  var actual = repository.GetAll().Count();
                  repository.Save();
                  var expected = repository.GetAll().Count();
                  Assert.AreNotEqual(actual, expected);
              }
  
              //AbstractFactoryPatternEntities context = new AbstractFactoryPatternEntities();
              //DbAuthorRepository repository = new DbAuthorRepository(context);
              //var actual = repository.GetAll().Count();
              //repository.Add(new AuthorEntity { FirstName = "Testing", LastName = "MyTest" });
              //repository.Save();
              //var expected = repository.GetAll().Count();
              //Assert.AreNotEqual(actual, expected);
          }
  
          [TestMethod]
          public void Remove_MethodIsCalled_EntityCountRemainsTheSame()
          {
              AbstractFactoryPatternEntities context = new AbstractFactoryPatternEntities();
              DbAuthorRepository repository = new DbAuthorRepository(context);
              AuthorEntity newAuthor = new AuthorEntity { FirstName = "Testing", LastName = "MyTest" };
              repository.Add(newAuthor);
              repository.Save();
              var actual = repository.GetAll().Count();
              Console.WriteLine(actual);
              repository.Remove(newAuthor);
              var expected = repository.GetAll().Count();
              Console.WriteLine(expected);
              Assert.AreEqual(actual, expected);
          }
  
          [TestMethod]
          public void Get_MethodIsCalled_CorrectAuthorIsRetrieved()
          {
              AbstractFactoryPatternEntities context = new AbstractFactoryPatternEntities();
              DbAuthorRepository repository = new DbAuthorRepository(context);
              int target = 4;
              AuthorEntity author = repository.GetById(target);
              Assert.AreEqual(target, author.AuthorId);
          }
  </code></pre>
  
  <p>I want to use shims/stub/fakes in order to make the test.</p>