ASP.NET Core MVC / Razor Pages: Testing
You can follow the ASP.NET Core Integration Tests documentation to learn details of ASP.NET Core integration tests. This document explains the additional test infrastructure provided by the ABP Framework.
The Application Startup Template
The Application Startup Template contains the .Web
project that contains UI views/pages/components of the application and a .Web.Tests
project to test these.
Testing the Razor Pages
Assume that you've created a Razor Page, named Issues.cshtml
with the following contents;
Issues.cshtml.cs
Issues.cshtml
This page simply creates a table with the issues:
You can write a test class inside the .Web.Tests
project just like the example below:
GetResponseAsStringAsync
is a shortcut method that comes from the base class that performs a HTTP GET request, checks if the resulting HTTP Status is 200
and returns the response as a string
.
You can use the base
Client
object (of typeHttpClient
) to perform any kind of request to the server and read the response yourself.GetResponseAsStringAsync
is just a shortcut method.
This example uses the HtmlAgilityPack library to parse the incoming HTML and test if it contains the issue table.
This example assumes there are some initial issues in the database. See the The Data Seed section of the Testing document to learn how to seed test data, so your tests can assume some initial data available in the database.
Testing the Controllers
Testing a controller is not different. Just perform a request to the server with a proper URL, get the response and make your assertions.
View Result
If the controller returns a View, you can use a similar code to test the returned HTML. See the Razor Pages example above.
Object Result
If the controller returns an object result, you can use the GetResponseAsObjectAsync
base method.
Assume that you've a controller as defined below:
You can write a test code to execute the API and get the result:
Testing the JavaScript Code
ABP Framework doesn't provide any infrastructure to test your JavaScript code. You can use any test framework and tooling to test your JavaScript code.
The Test Infrastructure
Volo.Abp.AspNetCore.TestBase package provides the test infrastructure that is integrated to the ABP Framework and ASP.NET Core.
Volo.Abp.AspNetCore.TestBase package is already installed in the
.Web.Tests
project.
This package provides the AbpWebApplicationFactoryIntegratedTest
as the fundamental base class to derive the test classes from. It's inherited from the WebApplicationFactory class provided by the ASP.NET Core.
The MyProjectWebTestBase
base class used above inherits from the AbpWebApplicationFactoryIntegratedTest
, so we indirectly inherited the AbpWebApplicationFactoryIntegratedTest
.
See Also