As we progress in our understanding of isolating dependencies with test doubles, in this lesson we introduce mocks, powerful tools for simulating external dependencies in software tests. In previous lessons, talked about Dummies and Stubs, which allow you to put something in the place of a dependency. However, mocks allow us to replicate the behavior of complex systems, enabling testing and verification in isolation without dependence on unpredictable systems like databases or web services.
Adhering to the TDD workflow remains crucial:
- Red: Start with a failing test.
- Green: Implement just enough code to pass the test.
- Refactor: Clean up your code without altering its functionality.
Using Go and Testify, we'll demonstrate how mocks are applied to effectively isolate and test application logic.
Mocks are essential in TDD, as they allow you to test code independently of system parts you don't control. When constructing tests for a PricingService
, for instance, you might mock an external currency conversion service to avoid failures due to downtime or unexpected changes in the API. Mocks create a controlled environment, where various conditions and responses can be simulated and calls can be validated.
Unlike stubs that merely return data, mocks fully simulate dependencies by preventing the actual code or functionality from executing and provide validation that the mock was called. For example, if a function interacts with an external API, a mock could simulate that API's response without making any network requests and verify the API was called with specific parameters.
We'll now explore mocking fundamentals using Testify. We'll initiate by understanding how to create mocks and set up expectations in Go.
Consider the ExchangeRateService
, responsible for fetching exchange rates from an API. When testing the PricingService
, mocking this service ensures that tests do not depend on real API interactions.
Below is an example using Testify's mock package for setting up a test:
In this structure:
- We define
IExchangeRateService
as an interface. MockExchangeRateService
is created using Testify'smock
package, imitating the behavior without implementing actual logic.mockService.On()
sets up the expectation and return value for the method.
We now implement the necessary code in PricingService
to pass the test:
This code ensures that the test passes by fulfilling the required logic for converting the price using the mocked exchange rate service.
If you want to mock a result with more than one set of inputs, you can simply configure it twice:
A more advanced case In Go, advanced mocking techniques involve sophisticated use cases, like ensuring the order of call execution, matching specific arguments, or handling complex scenarios with tools such as Testify:
This approach allows testing of various edge cases and provides flexibility to test alternative paths, ensuring robust real-world readiness. It is significantly more complicated than calling .On()
multiple times so should only be used in situations where you truly require a dynamic response in the mock
Validating Multiple Calls When creating multiple mock results, you can easily verify them using the following helpers:
AssertNumberOfCalls
will verify that the function was called exactly that many timesAssertExpectations
will verify that the mock configuration you set up was indeed called
This lesson covered the strategic use of mocks within the TDD framework, leveraging Go and Testify to enhance the reliability and focus of our application's logic:
- Red: Commence with a failing test to clarify the desired functionality.
- Green: Develop the minimal requisite code using mocks to satisfy the test.
- Refactor: Enhance and streamline the code without altering its functionality.
With these concepts at your disposal, you're prepared to continue with the practice exercises, further solidifying your proficiency. By mastering these approaches, you're advancing towards developing more robust, scalable applications. Keep practicing, and congrats on making it this far in the course!
