Controllers are a central part of any ASP.NET Core API service and ASP.NET MVC Web application. As such, you should have confidence they behave as intended for your application. Automated tests can provide you with this confidence and can detect errors before they reach production.
You need to test how the controller behaves based on valid or invalid inputs, and test controller responses based on the result of the business operation it performs. However, you should have these types of tests for your microservices:
Unit tests. These ensure that individual components of the application work as expected. Assertions test the component API.
Integration tests. These ensure that component interactions work as expected against external artifacts like databases. Assertions can test component API, UI, or the side effects of actions like database I/O, logging, etc.
Functional tests for each microservice. These ensure that the application works as expected from the user’s perspective.
Service tests. These ensure that end-to-end service use cases, including testing multiple services at the same time, are tested. For this type of testing, you need to prepare the environment first. In this case, it means starting the services (for example, by using docker-compose up).
Implementing unit tests for ASP.NET Core Web APIs
Unit testing involves testing a part of an application in isolation from its infrastructure and dependencies. When you unit test controller logic, only the content of a single action or method is tested, not the behavior of its dependencies or of the framework itself. Unit tests do not detect issues in the interaction between components—that is the purpose of integration testing.
As you unit test your controller actions, make sure you focus only on their behavior. A controller unit test avoids things like filters, routing, or model binding (the mapping of request data to a ViewModel or DTO). Because they focus on testing just one thing, unit tests are generally simple to write and quick to run. A well-written set of unit tests can be run frequently without much overhead.
Unit tests are implemented based on test frameworks like xUnit.net, MSTest, Moq, or NUnit. For the eShopOnContainers sample application, we are using xUnit.
When you write a unit test for a Web API controller, you instantiate the controller class directly using the new keyword in C#, so that the test will run as fast as possible. The following example shows how to do this when using xUnit as the Test framework.
[Fact] public async Task Get_order_detail_success() //Arrange var fakeOrderId = "12"; var fakeOrder = GetFakeOrder(); //... //Act var orderController = new OrderController( _orderServiceMock.Object, _basketServiceMock.Object, _identityParserMock.Object); orderController.ControllerContext.HttpContext = _contextMock.Object; var actionResult = await orderController.Detail(fakeOrderId); //Assert var viewResult = Assert.IsType<ViewResult>(actionResult); Assert.IsAssignableFrom<Order>(viewResult.ViewData.Model);
Implementing integration and functional tests for each microservice
As noted, integration tests and functional tests have different purposes and goals. However, the way you implement both when testing ASP.NET Core controllers is similar, so in this section we concentrate on integration tests.
Integration testing ensures that an application’s components function correctly when assembled. ASP.NET Core supports integration testing using unit test frameworks and a built-in test web host that can be used to handle requests without network overhead.
Unlike unit testing, integration tests frequently involve application infrastructure concerns, such as a database, file system, network resources, or web requests and responses. Unit tests use fakes or mock objects in place of these concerns. But the purpose of integration tests is to confirm that the system works as expected with these systems, so for integration testing you do not use fakes or mock objects. Instead, you include the infrastructure, like database access or service invocation from other services.
Because integration tests exercise larger segments of code than unit tests, and because integration tests rely on infrastructure elements, they tend to be orders of magnitude slower than unit tests. Thus, it is a good idea to limit how many integration tests you write and run.
ASP.NET Core includes a built-in test web host that can be used to handle HTTP requests without network overhead, meaning that you can run those tests faster when using a real web host. The test web host (TestServer) is available in a NuGet component as Microsoft.AspNetCore.TestHost. It can be added to integration test projects and used to host ASP.NET Core applications.
As you can see in the following code, when you create integration tests for ASP.NET Core controllers, you instantiate the controllers through the test host. This is comparable to an HTTP request, but it runs faster.
public class PrimeWebDefaultRequestShould private readonly TestServer _server; private readonly HttpClient _client; public PrimeWebDefaultRequestShould() // Arrange _server = new TestServer(new WebHostBuilder() .UseStartup<Startup>()); _client = _server.CreateClient(); [Fact] public async Task ReturnHelloWorld() // Act var response = await _client.GetAsync("/"); response.EnsureSuccessStatusCode(); var responseString = await response.Content.ReadAsStringAsync(); // Assert Assert.Equal("Hello World!", responseString);
Implementing service tests on a multi-container application
As noted earlier, when you test multi-container applications, all the microservices need to be running within the Docker host or container cluster. End-to-end service tests that include multiple operations involving several microservices require you to deploy and start the whole application in the Docker host by running docker-compose up (or a comparable mechanism if you are using an orchestrator). Once the whole application and all its services is running, you can execute end-to-end integration and functional tests.
There are a few approaches you can use. In the docker-compose.yml file that you use to deploy the application at the solution level you can expand the entry point to use dotnet test. You can also use another compose file that would run your tests in the image you are targeting. By using another compose file for integration tests that includes your microservices and databases on containers, you can make sure that the related data is always reset to its original state before running the tests.
Once the compose application is up and running, you can take advantage of breakpoints and exceptions if you are running Visual Studio. Or you can run the integration tests automatically in your CI pipeline in Azure DevOps Services or any other CI/CD system that supports Docker containers.
Testing in eShopOnContainers
The reference application (eShopOnContainers) tests were recently restructured and now there are four categories:
Unit tests, just plain old regular unit tests, contained in the MicroserviceName.UnitTests projects
Microservice functional/integration tests, with test cases involving the infrastructure for each microservice but isolated from the others and are contained in the MicroserviceName.FunctionalTests projects.
Application functional/integration tests, that focus on microservices integration, with test cases that exert several microservices. These tests are located in project Application.FunctionalTests.
Load tests, that focus on response times for each microservice. These tests are located in project LoadTest and need Visual Studio 2017 Enterprise Edition.
Unit and integration test per microservice are contained in a test folder in each microservice and Application a Load tests are contained under the test folder in the solution folder, as shown in Figure 6-25.
Figure 6-25. Test folder structure in eShopOnContainers
Microservice and Application functional/integration tests are run from Visual Studio, using the regular tests runner, but first you need to start the required infrastructure services, by means of a set of docker-compose files contained in the solution test folder:
version: '3.4' services: redis.data: image: redis:alpine rabbitmq: image: rabbitmq:3-management-alpine sql.data: image: microsoft/mssql-server-linux:2017-latest nosql.data: image: mongo
version: '3.4' services: redis.data: ports: - "6379:6379" rabbitmq: ports: - "15672:15672" - "5672:5672" sql.data: environment: - SA_PASSWORD=Pass@word - ACCEPT_EULA=Y ports: - "5433:1433" nosql.data: ports: - "27017:27017"
So, to run the functional/integration tests you must first run this command, from the solution test folder:
docker-compose -f docker-compose-test.yml -f docker-compose-test.override.yml up
As you can see, these docker-compose files only start the Redis, RabbitMQ, SQL Server and MongoDB microservices.