Unit testing controllers
Unit testing controllers
Set up unit tests of controller actions to focus on the controller's behavior. A controller unit test avoids scenarios such as filters, routing, and model binding. Tests that cover the interactions among components that collectively respond to a request are handled by integration tests. For more information on integration tests, see Integration tests in ASP.NET Core.
If you're writing custom filters and routes, unit test them in isolation, not as part of tests on a particular controller action. To demonstrate controller unit tests, review the following controller in the sample app.
Testing built packages locally with moq
moq
You can either build from command line or explicitly Pack (from the context menu) the Moq.Package project.
Packages are generated in the bin
folder in the repository root. To test these packages you can just add a package source pointing to it. You can also just place a NuGet.Config
like the following anywhere above the directory with the test solution(s):
Walkthrough: Create and run unit tests for managed code
Create a unit test project
On the File menu, select Add > New Project.
Type unit test in the search box, select C# as the language, and then select the C# Unit Test Project for .NET Core template, and then click Next.
Name the project BankTests and click Next.
Choose either the recommended target framework (.NET Core 3.1) or .NET 5, and then choose Create. The BankTests project is added to the Bank solution.
In the BankTests project, add a reference to the Bank project. In Solution Explorer, select Dependencies under the BankTests project and then choose Add Reference from the right-click menu.
In the Reference Manager dialog box, expand Projects, select Solution, and then check the Bank item.
Choose OK.
To keep reading more about simple test environment in c# please take a look here
Last updated