Search⌘ K
AI Features

Testing the Application and Domain with Unit Tests

Explore how to write effective unit tests for Go applications by testing individual functions in isolation. Learn to use test doubles like mocks and stubs to replace dependencies and apply table-driven testing to simplify multiple test scenarios. This lesson helps you understand testing strategies to enhance the reliability and maintainability of event-driven Go applications.

The system under test for a unit test is the smallest unit we can find in our application. In applications that are written in Go, this unit will be a function or method on a struct:

The scope of a unit test
The scope of a unit test

As shown in the above figure, only the function code is being tested. Any dependencies that the code under test requires must be provided as a test double such as a mock, a stub, or a fake dependency.

Each test should focus on testing only one path through the function. Even for moderately complex functions, this can result in a lot of duplication in ...