title: How to create an Entity Framework DbContext for unit testing in TDD?
body: <p>I'm in the process of learning about Test Driven Development (TDD) so I can use it in a project I've started, but I've run into a question on how to set up a specific type of test I'd like to do.</p>
  
  <p>The scenario is that I have a View that allows someone to edit information about a User (such as username, first name, last name, etc). In many scenarios this User they're editing will already exist in the database, so when they hit save the information gets updated in the database by that View's View Model.</p>
  
  <p>What I'd like to test is that the View Model is saving this information to the database. This is done using an Entity Framework DbContext I pass into the View Model during construction, which means that I need to create a DbContext in the unit test to pass into the View Model that can be updated and compared against. </p>
  
  <p>The Assert I'd like to test would be something along the lines of:</p>
  
  <pre><code>Assert.AreEqual(ViewModelFake.EditedUser.Username, DataBaseContextFake.Users.Find(1).Username);
  </code></pre>
  
  <p>After the DbContext is originally created by the unit test it populates it with a User, and later in the unit test that User information is changed. The command that's being tested in the View Model is responsible for saving this edited information into the database, replacing what the DbContext was originally populated with.</p>
  
  <p>I've been searching for a solution for the better part of these last two days but haven't been able to track down examples of people doing the same thing. Is this something that should even be handled in a unit test? Please note that I'm not using a repository/unit of work layer on top of Entity Framework.</p>