title: Test if save method is hit in unit test
body: <p>I'm starting with unit testing in the asp.net mvc 4 framework.</p>
  
  <p>I got a repository with basic crud methods and a save method. When I create a unit test I create a test repository and test if e.g. a item to the collection is added. That all goes smoothly but I cannot test if the save method is hit. </p>
  
  <p>I tried to add a boolean property to the test repository which will be set to true if .save() is hit. But then I need to change the interface, and also the database repository. Which is in my opinion neither practical nor best practice.</p>
  
  <p>What is the best method to test this? Thank you in advance for your answer.</p>
  
  <p>My code:</p>
  
  <p>the fake repository:</p>
  
  <pre><code>public class TestUserRepository : IUserManagementRepository
      {
          /// &lt;summary&gt;
          /// entries used used for testing
          /// &lt;/summary&gt;
          private List&lt;User&gt; _entities;
  
          /// &lt;summary&gt;
          /// constructor
          /// &lt;/summary&gt;
          public TestUserRepository()
          {
              _entities = new List&lt;User&gt;();
              _entities.Add(new User
              {
                  Id = 1,
                  InsertDate = DateTime.Now,
                  LastUpdate = DateTime.Now,
                  Username = "TestUserName",
                  Password = "TestPassword"
              });
          }
  ...
  
  public void Create(User task)
          {
              _entities.Add(task);
          }
  
  public void Save()
          {
              //do nothing
          }
      }
  </code></pre>
  
  <p>the controller to test:</p>
  
  <pre><code>[HttpPost]
          public ActionResult Create(User user)
          {
              if (ModelState.IsValid)
              {
                  _repository.Create(user);
                  _repository.Save();
                  return RedirectToAction("Index");
              }
              else
              {
                  return View(user);
              }
  
          }
  </code></pre>
  
  <p>and the test</p>
  
  <pre><code>[TestMethod()]
          public void CreateTest()
          {
              IUserManagementRepository repository = new TestUserRepository();
              UserController controller = new UserController(repository);
              User user = new User { Username = "UnitTestUserName", InsertDate = DateTime.Now, LastUpdate = DateTime.Now, Password = "Password" };
              ActionResult actionResult = controller.Create(user);
          User returnedUser = repository.FindBy(u =&gt; u.Username == "UnitTestUserName").First&lt;User&gt;();
  
              Assert.IsNotNull(actionResult);
              Assert.AreEqual(user, returnedUser);            
          }
  </code></pre>