

Notice that the controller includes a constructor that takes as a parameter a list of Product objects. The list defined in this class represents the production data. To simplify this example, the data is stored in a list rather than a database. Replace the existing code with the following code. Set the controller name to SimpleProductController, and click Add. Right-click the Controllers folder and select Add and New Scaffolded Item. Replace the contents of the file with the following code.

In your StoreApp project, add a class file to the Models folder named Product.cs. In the unit test project, add a project reference to the original project. You will see the unit test project in your solution. Select Test in the left pane, and select Unit Test Project for the project type. To add a unit test project, right-click your solution and select Add and New Project. For example, suppose you already have an application named StoreApp, and you want to add unit tests. If you did not create the unit test project when you created your application, you can add one at any time. You can keep this name.Īfter creating the application, you will see it contains two projects.Īdd unit test project to an existing application The unit test project is automatically named StoreApp.Tests. In the New ASP.NET Project windows, select the Empty template and add folders and core references for Web API.


To follow this tutorial, you can use either approach.Īdd unit test project when creating the applicationĬreate a new ASP.NET Web Application named StoreApp. This tutorial shows both methods for creating a unit test project. You can either create a unit test project when creating your application or add a unit test project to an existing application. The downloadable project includes unit test code for this topic and for the Mocking Entity Framework when Unit Testing ASP.NET Web API topic.Ĭreate application with unit test project Visual Studio 2017 Community, Professional or Enterprise editionĭownload the completed project. Add unit test project to an existing application.Add unit test project when creating the application.Create application with unit test project.This topic contains the following sections: For unit testing more advanced data scenarios, see Mocking Entity Framework when Unit Testing ASP.NET Web API 2. The unit tests in this topic are intentionally limited to simple data scenarios. For an introductory tutorial, see Getting Started with ASP.NET Web API 2. This tutorial assumes you are familiar with the basic concepts of ASP.NET Web API. This tutorial shows how to include a unit test project in your solution, and write test methods that check the returned values from a controller method. This guidance and application demonstrate how to create simple unit tests for your Web API 2 application.
