title: Moq and Entity framework
body: <p>I am trying to find out the best way to do this - I have searched ALOT for a few hours but cannot get this to work. I need a fresh pair of eyes and perspective.</p>
  
  <p>I am trying to create a straight forward app. 
  It will be using EF 6.1.0 as the DAL. </p>
  
  <p>I have an entity called User.
  I have another entity called WorkItem</p>
  
  <p>a user can have many WorkItems.</p>
  
  <p>I have created a EFDbContext called "TimesheetsContext". It inherits from DbContext and has 2 virtual properties:</p>
  
  <pre><code>    public virtual IDbSet&lt;WorkItem&gt; WorkItems { get; set; }
  
      public virtual IDbSet&lt;User&gt; Users { get; set; }
  </code></pre>
  
  <p>I also have an IUnitOfWork (and its concrete class) </p>
  
  <pre><code>public interface IUnitOfWork
  {
      ITimeSheetContext Context { get; set; }
      IWorkItemRepository WorkItemRepository { get; set;  }
      IUserRepository UserRepository { get; set; }
  }
  </code></pre>
  
  <p>The WorkItem and User repository simply has functions such as Add, Login, Update, which in turn calls the EfDBContext to perform the request operation.</p>
  
  <p>Now, I want to create a unit test to be able to mock up the DbContext and be able to add users or work items. However, I cannot seem to get it to work.</p>
  
  <p>Once, with your help, I have got this far, I can then easily change it to use a services layer and pass in an IUnitOfWork.</p>
  
  <p>Using Moq and EntityFramework.Testing.Moq, I cannot get it to work:</p>
  
  <pre><code>    private Mock&lt;TimeSheetContext&gt; _mockContext;
  
      // &lt;snip /&gt;
  
          Guid userId1 = Guid.NewGuid();
          Guid userId2 = Guid.NewGuid();
          Guid userId3 = Guid.NewGuid();
          var users = new List&lt;User&gt;(new[] 
          { 
              new User { Firstname = "Joe", Lastname = "Bloggs", Password = "pass1", UserId = userId1, Username = "JoeBloggs" },
              new User { Firstname = "Thom", Lastname = "Stevens", Password = "pass2", UserId = userId2, Username = "ThomStevens"},
              new User { Firstname = "Homer", Lastname = "Simpson", Password = "pass3", UserId = userId3, Username = "HomerSimpson" }
          }).AsQueryable();
  
          var tasks = new List&lt;LionTask&gt;(new[] 
          { 
              new WorkItem { CreatedDate = DateTime.Today, Description = "Desc1", ModifiedDate = DateTime.Today, State = 1, TaskId = Guid.NewGuid(), Title = "Test Title", UserId = userId1 },
              new WorkItem { CreatedDate = DateTime.Today, Description = "Desc2", ModifiedDate = DateTime.Today, State = 2, TaskId = Guid.NewGuid(), Title = "Test Title 2", UserId = userId2 },
              new WorkItem { CreatedDate = DateTime.Today, Description = "Desc3", ModifiedDate = DateTime.Today, State = 3, TaskId = Guid.NewGuid(), Title = "Test Title 3", UserId = userId3 }
          }).AsQueryable();
  
          this._mockContext = new Mock&lt;TimeSheetContext&gt;();
          var taskDbSetMock = new Mock&lt;IDbSet&lt;WorkItem&gt;&gt;();
          var userDbSetMock = new Mock&lt;IDbSet&lt;User&gt;&gt;();
          userDbSetMock.As&lt;IQueryable&lt;User&gt;&gt;().Setup(m =&gt; m.Provider).Returns(users.Provider);
          userDbSetMock.As&lt;IQueryable&lt;User&gt;&gt;().Setup(m =&gt; m.Expression).Returns(users.Expression);
          userDbSetMock.As&lt;IQueryable&lt;User&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(users.ElementType);
          userDbSetMock.As&lt;IQueryable&lt;User&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(users.GetEnumerator());
  
          taskDbSetMock.As&lt;IQueryable&lt;WorkItem&gt;&gt;().Setup(m =&gt; m.Provider).Returns(tasks.Provider);
          taskDbSetMock.As&lt;IQueryable&lt;WorkItem&gt;&gt;().Setup(m =&gt; m.Expression).Returns(tasks.Expression);
          taskDbSetMock.As&lt;IQueryable&lt;WorkItem&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(tasks.ElementType);
          taskDbSetMock.As&lt;IQueryable&lt;WorkItem&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(tasks.GetEnumerator());
  
          this._mockContext.Setup(c =&gt; c.Users).Returns(userDbSetMock.Object);
          this._mockContext.Setup(c =&gt; c.WorkItems).Returns(taskDbSetMock.Object);
  </code></pre>
  
  <p>Finally, I then have a test like this but when I add a user, I still get back 3 and the Assert fails:</p>
  
  <pre><code>        User u = new User { Firstname = "Timmy", Lastname = "Johanson", Password = "omg123", UserId = Guid.NewGuid(), Username = "TJ" };
          this._mockContext.Object.Users.Add(u);
  
          Assert.AreEqual(4, this._mockContext.Object.Users.Count());
  </code></pre>
  
  <p>am I going about this the wrong way? </p>