How to do it...

  1. First, let's create a class library project in the solution:
  1. Next, we will change the generated code in the project to import xunit, dotnet-test-xunit, and moq. We will also have to add the reference on the Web API project.
  1. Here are some of the test methods:
public class ProductApiControllerTests{  #region Tests for GET : api/productapi  [Fact]  public void GET  _Returns404NotFoundResultIfProductListHaveNoItemsInRepo()  {    // Arrange    var mockRepo = new Mock<IProductRepository>();    var emptyProductList = GetEmptyProductsList();    mockRepo.Setup(repo => repo.GetAllProducts())    .Returns(emptyProductList); var controller = new ProductApiController(mockRepo.Object); ...

Get ASP.NET Core MVC 2.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.