title: Returning a collection via a repository using Moq
body: <p>I'm relatively new to unit testing and ASP.NET MVC as a whole and I'm trying to write my first unit test against a simple controller action and a repository (as seen below) using <code>Moq</code>.</p>
  
  <p><strong>ISubmissionRepository.cs</strong></p>
  
  <pre><code>public interface ISubmissionRepository
  {
      IList&lt;Submission&gt; GetRecent(int limit = 10);
  }
  </code></pre>
  
  <p><strong>HomeController.cs:</strong></p>
  
  <pre><code>/* Injected using Unit DIC */
  public HomeController(ISubmissionRepository submissionRepository)
  {
      _submissionRepo = submissionRepository;
  }
  
  public ActionResult Index()
  {
  
      var latestList = _submissionRepo.GetRecent();
      var viewModel = new IndexViewModel {
          NumberOfSubmissions = latestList.Count(),
          LatestSubmissions = latestList
      };
      return View(viewModel);
  }
  </code></pre>
  
  <p>Below is the unit test I'm writing, however my mocked repository call doesn't appear to be returning anything and I've no idea why. Am I mocking my repository call correctly?</p>
  
  <p><strong>HomeControllerTest.cs</strong></p>
  
  <pre><code>[Test]
  public void Index()
  {
      IList&lt;Submission&gt; submissions = new List&lt;Submission&gt;
      {
          new Submission {Credit = "John Doe", Description = "Hello world", ID = 1, Title = "Example Post"},
          new Submission {Credit = "John Doe", Description = "Hello world", ID = 2, Title = "Example Post"}
      };
  
      Mock&lt;ISubmissionRepository&gt; mockRepo = new Mock&lt;ISubmissionRepository&gt;();
      mockRepo.Setup(x =&gt; x.GetRecent(2)).Returns(submissions);
  
      /* 
      * This appears to return null when a breakpoint is set
      var obj = mockRepo.Object;
      IList&lt;Submission&gt; temp = obj.GetRecent(2);
      */
  
      controller = new HomeController(mockRepo.Object);
      ViewResult result = controller.Index() as ViewResult;
  
      Assert.NotNull(result);
      Assert.IsInstanceOf&lt;IndexViewModel&gt;(result);
  
  }
  </code></pre>