title: Unit test for MVC 5 async Task<ActionResult> Index view
body: <p>I am new to unit testing. Not sure what to do my testing on, I went ahead and started one with an Index view on ClientController. I am having issues writing test method for the Index view which is declared as async Task. What i want to do is make sure this action returns data. Not sure how important this test is but I wanted to learn ways to write tests in MVC 5. Seems to have trouble writing tests for the view that are async.</p>
  
  <p>ManagementService class</p>
  
  <pre><code>public IQueryable&lt;Client&gt; GetAllClients(bool includeDeleted = false)
      {
          var query = context.Clients.AsQueryable();
          if (!includeDeleted) query = query.Where(c =&gt; !c.IsDeleted);
          return query;
      }
  </code></pre>
  
  <p>Client Controller</p>
  
  <pre><code>public ClientController(IApplicationContext appContext, IManagementService adminService, IDomainService domainService)
      {
          this.adminService = adminService;
          this.appContext = appContext;
          this.domainService = domainService;
      }
  public async Task&lt;ActionResult&gt; Index()
      {
          var clients = adminService.GetAllClients();
          return View(await clients.ToListAsync());
      }
  </code></pre>
  
  <p>//TEST</p>
  
  <pre><code>[TestMethod]
      public void Index()
      {
          //Arrange
          var mgmtmockContext = new Mock&lt;IManagementService&gt;();
          List&lt;Client&gt; db = new List&lt;Client&gt;();
          db.Add(new Client { ClientId = 1, Name = "Test Client 1" });
          db.Add(new Client { ClientId = 2, Name = "Test Client 2" });
          mgmtmockContext
              .Setup(m =&gt; m.GetAllClients(false))
              .Returns(() =&gt; { return db.AsQueryable(); });
  
          //Act
          ClientController controller = new ClientController(null, mgmtmockContext.Object, null);
  
          var result = controller.Index() as Task&lt;ActionResult&gt;;
  
          //Assert
          Assert.IsNotNull(result);
      }
  </code></pre>
  
  <p>After I create an instance of the controller, I am not sure how to pass the mock object to Index() so I can test this view returns data.</p>
  
  <p>What I want to Assert is Assert.AreEqual(2, result.Count()) so I can run this test. </p>