title: ASP.Net Core unit testing async controller
body: <p>I have this test:</p>
  
  <pre><code>[Fact]
  public async void Can_Paginate()
  {
      //fake data
      var product1 = new Product { ProductId = 1, ProductName = "P1" };
      var product2 = new Product { ProductId = 2, ProductName = "Product 2" };
      var product3 = new Product { ProductId = 3, ProductName = "Product 3" };
      var product4 = new Product { ProductId = 4, ProductName = "Product 4" };
      var product5 = new Product { ProductId = 5, ProductName = "Product 5" };
      var data = new List&lt;Product&gt;
      {
           product1,
           product2,
           product3,
           product4,
           product5
       }.ToAsyncEnumerable();
  
        var category1 = new Category { CategoryId = 1 };
  
        var prodCategoryData = new List&lt;ProductCategory&gt;
        {
            new ProductCategory { Product =product1,Category = category1},
            new ProductCategory { Product =product2,Category = category1},
            new ProductCategory { Product =product3,Category = category1},
            new ProductCategory { Product =product4,Category = category1},
            new ProductCategory { Product =product5,Category = category1}
        }.ToAsyncEnumerable();
  
        var dbSetMock = new Mock&lt;DbSet&lt;Product&gt;&gt;();
        //dbSetMock.As&lt;IQueryable&lt;Product&gt;&gt;().Setup(m =&gt; m.Provider).Returns(data.Provider);
        //dbSetMock.As&lt;IQueryable&lt;Product&gt;&gt;().Setup(m =&gt; m.Expression).Returns(data.Expression);
        //dbSetMock.As&lt;IQueryable&lt;Product&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(data.ElementType);
        //dbSetMock.As&lt;IQueryable&lt;Product&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(data.GetEnumerator());
  
        var pcDbSetMock = new Mock&lt;DbSet&lt;ProductCategory&gt;&gt;();
        // pcDbSetMock.As&lt;IQueryable&lt;ProductCategory&gt;&gt;().Setup(m =&gt; m.Provider).Returns(prodCategoryData.Provider);
        //pcDbSetMock.As&lt;IQueryable&lt;ProductCategory&gt;&gt;().Setup(m =&gt; m.Expression).Returns(prodCategoryData.Expression);
        //pcDbSetMock.As&lt;IQueryable&lt;ProductCategory&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(prodCategoryData.ElementType);
        //pcDbSetMock.As&lt;IQueryable&lt;ProductCategory&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(prodCategoryData.GetEnumerator());
  
        Mock&lt;ApplicationDbContext&gt; customDbContextMock = new Mock&lt;ApplicationDbContext&gt;();
        customDbContextMock.Setup(x =&gt; x.Products).Returns(dbSetMock.Object);
        customDbContextMock.Setup(x =&gt; x.ProductCategories).Returns(pcDbSetMock.Object);
  
        var loggerMock = new Mock&lt;ILogger&lt;ProductsController&gt;&gt;();
        var envMock = new Mock&lt;IHostingEnvironment&gt;();
        var httpContext = new Mock&lt;HttpContext&gt;().Object;
        var actionDescriptor = new Mock&lt;Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor&gt;().Object;
        var modelState = new ModelStateDictionary();
        var actionContext = new ActionContext(httpContext, new Mock&lt;RouteData&gt;().Object, actionDescriptor, modelState);
  
        ProductsController controller = new ProductsController(customDbContextMock.Object, loggerMock.Object, envMock.Object);
        controller.pageSize = 3;
        controller.ControllerContext = new ControllerContext(actionContext);
        var result = await controller.List(1, 2);
  </code></pre>
  
  <p>My controller logic:</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; List(int Id, int page = 1)
  {
  
     if (Id == 0)
     {
         ViewBag.CategoryName = "Wszystkie produkty";
         return View(await _context.ProductCategories.Include(c =&gt; c.Product).ToListAsync());
     }
  
     return View(await _context.ProductCategories
         .Include(p =&gt; p.Product)
         .Where(pt =&gt; pt.CategoryId == Id)
         .OrderBy(p=&gt;p.ProductId)
         .Skip((page-1)*pageSize)
         .ToListAsync());
  }
  </code></pre>
  
  <p>My problem is that I cannot mock <code>_context.ProductCategories</code> by using <code>prodCategoryData</code> and data lists as <code>IAsyncEnumerable</code>.</p>
  
  <p>When I cast them to <code>IQueryable</code> I get this error instead:</p>
  
  <pre><code>Additional information: The source IQueryable doesn't implement IAsyncEnumerable&lt;WineCom.Models.ProductCategory&gt;. Only sources that implement IAsyncEnumerable can be used for Entity Framework .
  </code></pre>