title: Unit testing controller with dependency injected service and repository
body: <p>I have a pretty standard .NET MVC 3 application that uses Ninject for dependency injection.  What I'm trying to unit test is the view that gets returned from the controller.  There is a controller with a group service on it and the service has a repository.  The service first checks to see if that group exists and if it does, it returns false along with an error.  The controller then checks the boolean value to determine whether to move the user on to the listing of groups or stay on the current page and display a value.</p>
  
  <p><strong>Controller</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>[HttpPost]
  public ActionResult AddGroup(Group g)
  {
     string error = string.Empty;
     if (groupService.Save(g, CurrentUser, out error))
     {
         return RedirectToAction("GetGroups");
     }
     else
     {
         ViewData["Error"] = error;
         return View("AddGroup");
     }
  }
  </code></pre>
  
  <p><strong>Service</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>[Inject]
  public IGroupRepository CurrentGroupRepo { get; set; }
  
  public bool Save(Group group, string username, out string error)
  {
      error = string.Empty;
      var found = CurrentGroupRepo.GetGroup(group.Descr, false);
      if (found != null)
      {
          error = "That group alread exists";
          return false;
      }
  
      if (group.CreatedBy == null || group.CreatedBy == string.Empty)
      {
          group.CreatedBy = username;
          group.CreatedOn = DateTime.Now;
          group.IsDeleted =false;
      }
  
      group.ModifiedBy = username;
      group.ModifiedOn = DateTime.Now;
  
      try
      {
          if (group.GroupID == 0)
          {
              CurrentGroupRepo.AddGroup(group);
          }
          else
          {
              CurrentGroupRepo.UpdateGroup(group);
          }
          return true;
      }
      catch (Exception ex)
      {
          error = ex.Message + ex.StackTrace;
          return false;
      }
  }
  </code></pre>
  
  <p><strong>Unit Test</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>[TestMethod]
  public void TestAddGroup()
  {
      // Arrange
      Mock&lt;IUserService&gt; uService = new Mock&lt;IUserService&gt;();
      Mock&lt;IGroupService&gt; gService = new Mock&lt;IGroupService&gt;();
      Mock&lt;IObservationService&gt; oService = new Mock&lt;IObservationService&gt;();
      Mock&lt;IGroupRepository&gt; mockGroupRepo = new Mock&lt;IGroupRepository&gt;();
  
          string error = string.Empty;
          List&lt;Group&gt; groups = new List&lt;Group&gt;();
          Group newGroup = new Group() { Descr = "Test Group 1234566", IsDeleted = false };
          Mock&lt;IGroupRepository&gt; mockGroupRepo = new Mock&lt;IGroupRepository&gt;();
  
          mockGroupRepo.Setup(cr =&gt; cr.GetGroups(It.IsAny&lt;bool&gt;())).Returns(
              delegate {
                  return groups;
              });
          mockGroupRepo.Setup(cr =&gt; cr.GetGroup(It.IsAny&lt;string&gt;(), It.IsAny&lt;bool&gt;())).Returns(
              delegate(Group _group) {
                  return groups.Where(f =&gt; f.Descr == _group.Descr).FirstOrDefault();
              });
          mockGroupRepo.Setup(cr =&gt; cr.AddGroup(newGroup)).Returns(
              delegate {
                  groups.Add(newGroup);
                  return newGroup;
              });
          gService.SetupGet(d =&gt; d.CurrentGroupRepo).Returns(mockGroupRepo.Object);
  
  
      AdminController controller = new AdminController(gService.Object, uService.Object, oService.Object);
      Group newGroup = new Group() { Descr = "Test Group 1234566", IsDeleted = false };
      var success = (ViewResult)controller.AddGroup(newGroup);
      Assert.IsTrue("GetGroups" == success.ViewName);
  
      var failure = (ViewResult)controller.AddGroup(newGroup);
      Assert.IsTrue("AddGroup" == failure.ViewName);
  }
  </code></pre>
  
  <p>What I'm trying to test is that when I add one group, it should work and then when I add the same group, it should go to a different view.  But right now, the unit test is using the real repository instead of a mocked one.  How do I get the repository on the mocked service to be controlled by the unit test instead of actually using a real repository?</p>