Testing Controllers, Messages & Systems with Andrew Lock
Sep 25, 2020
auto_awesome
Andrew Lock joins us to talk about testing. They discuss testing API/MVC controllers, messaging, databases, and more. The chapter explores challenges in testing controllers, messages, and systems, including the difficulties of testing long-running processes and the need to ensure appropriate handlers are present.
Unit testing controllers may have limited value due to many of their aspects being provided by the framework and external to the controller itself.
A combination of unit tests and integration tests should be employed based on the specific context and value that each test brings.
Deep dives
The Value of Testing Controller Logic
The episode discusses the value of testing controller logic in a .NET context. While some argue that integration tests are more valuable than unit tests, the hosts question the necessity of unit testing controllers. They recognize that many aspects of controllers, such as model binding, validation, and routing, are provided by the framework and can be considered external to the controller itself. They also highlight the limitations of testing thick controllers that directly manipulate databases and recommend a thin controller approach using mediators or dispatchers. They emphasize the importance of segmenting testing, with unit tests focusing on isolated units of code and integration tests handling the end-to-end scenarios.
Different Approaches and Trade-offs in Testing
The hosts discuss the various approaches and trade-offs involved in testing different components of a system. They acknowledge that testing is valuable, but it's essential to consider the specific context and the value that each type of test brings. They mention that it's not a one-size-fits-all approach and that a combination of unit tests and integration tests can be employed based on the situation. They also explore the challenges of testing long-running processes, such as those involving message queues like Kafka, and weigh the pros and cons of mocking versus full integration testing.
Orchestrating Events and Business Logic Testing
The conversation delves into the orchestration of events and testing the associated business logic. They discuss the challenges of ensuring that events trigger expected reactions in distributed systems, such as when using Kafka. While they acknowledge the value of testing event emissions, they note that the primary focus is on verifying that the events were published correctly rather than fully testing the process. They also touch on the notion of value-driven testing and emphasize that different testing approaches might be necessary depending on the business requirements and architectural patterns of the application.
The Cost and Benefit of Testing Distributed Systems
The hosts explore the cost and benefit of testing distributed systems and the trade-offs involved. They highlight the challenges of setting up and maintaining an entire distributed system for integration testing and express concerns about the coupling it creates. They discuss the importance of designing autonomous components that can function independently and the need to balance the desire for complete system testing with the practicality of maintaining efficient and scalable test suites. They emphasize the importance of considering the context, value, and risks associated with testing distributed systems.
Andrew Lock joins us to talk about testing. We started out by talking about should you test API/MVC controllers? The discussion then turned into testing for messaging, databases, and much more.
Should you unit-test API/MVC controllers in ASP.NET Core?