title: Create unit test for View() with a List in ASP.net core MVC application
body: <p>I am new in unit testing. I have a controller - <code>StudentsController</code> with dependency injection and there my <code>Index()</code> method:</p>
  
  <pre><code>public class StudentsController : Controller
  {
      public readonly UniversityContext _context;//Database
      public StudentsController(UniversityContext context)
      {
          _context = context;    
      }//Constructor with database
      // GET: Students
      public async Task&lt;IActionResult&gt; Index()
      {
          return View(await _context.Students.ToListAsync());
      }
  }
  </code></pre>
  
  <p>Next i need to write a correct unit test code, that check, if:</p>
  
  <blockquote>
    <p>1) a View() have a list with my students</p>
    
    <p>2) The query with students is not null.</p>
  </blockquote>
  
  <p>I read about Mock objects, but I don't know how to write the correct code. My code that I wrote so far:</p>
  
  <pre><code>public class StudentsControllerTests
  {
      [Fact]
      public async Task Index_ReturnsAViewResult_WithAListOfStudents()
      {
          var mockRepo = new Mock&lt;UniversityContext&gt;();
          mockRepo.Setup(repo =&gt; repo.Students.ToList()).Returns(GetTestStudents());//There i get following error:Expression references a method that does not belong to the mocked object
          var controller = new StudentsController(mockRepo.Object);
  
          // Act
          var result = controller.Index();
  
          //// Assert
          var viewResult = Assert.IsType&lt;ViewResult&gt;(result);
          var model = Assert.IsAssignableFrom&lt;IEnumerable&lt;Student&gt;&gt;(
              viewResult.ViewData.Model);
          Assert.NotNull(model);//Second Condition
  
      }
      public List&lt;Student&gt; GetTestStudents()
      {
          var sessions = new List&lt;Student&gt;();
          sessions.Add(new Student()
          {
              bDate = new DateTime(1994, 7, 2),
              Name = "Test One"
          });
          sessions.Add(new Student()
          {
              bDate = new DateTime(1995, 7, 1),
              Name = "Test Two"
          });
          return sessions;
      }
  }
  </code></pre>
  
  <p>Can someone explain me, how to correct my code?</p>