title: Setting up mock objects for EF dbcontext to test repository methods
body: <p>I have entityframework repo which gets trip information from sqldb. I have created the repo and on constructor injecting the dbContext and using that context doing database operation.</p>
  
  <pre><code> public class WorldRepository : IWorldRepository
      {
          private WorldContext _context;
  
          public WorldRepository(WorldContext context)
          {
              _context = context;
          }
  
          public void AddSop(string tripName, Stop newStop)
          {
              var trip = GetTipByName(tripName);
  
              if (trip != null)
              {
                  trip.Stops.Add(newStop);
                  _context.Stops.Add(newStop);
              }
          }
  
          public void AddTrip(Trip trip)
          {
              _context.Add(trip);
          }
  
          public IEnumerable&lt;Trip&gt; GetAllTrips()
          {
              return _context.Trips.ToList();
          }
  }
  </code></pre>
  
  <p>Now I am trying to test using MOQ, but this does not help . I am not able to test any of logic writen on my methods as it's querying mock object and not my implementation.</p>
  
  <pre><code>  // private Mock&lt;IWorldRepository&gt; _mockWorld;
  
          [TestMethod]
          public void Test_AddTrips()
          {
              //Arrange
              // WorldRepository repo = new WorldRepository(null);
  
              Mock&lt;IWorldRepository&gt; _mockWorld = new Mock&lt;IWorldRepository&gt;();
              var repo = _mockWorld.Object;
  
              //Act
              repo.AddSop("Sydney", new Stop
              {
                  Arrival = DateTime.Now,
                  Id = 2,
                  Latittude = 0.01,
                  Longitude = 0.005,
                  Name = "Test Trip",
                  Order = 5
              });
  
              repo.SaveChangesAsync();
  
              var count = repo.GetAllTrips().Count();
  
              //Assert
              Assert.AreEqual(1, count);
  
  
          }
  </code></pre>
  
  <p>This is code for WorldContext.</p>
  
  <pre><code>public class WorldContext:DbContext
      {
          private IConfigurationRoot _config;
  
          public WorldContext(IConfigurationRoot config,DbContextOptions options)
              :base(options)
          {
              _config = config;
          }
  
          public DbSet&lt;Trip&gt; Trips { get; set; }
          public DbSet&lt;Stop&gt; Stops{ get; set; }
  
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              base.OnConfiguring(optionsBuilder);
              optionsBuilder.UseSqlServer(_config["ConnectionStrings:WorldCotextConnection"]);
          }
      }
  </code></pre>