November 2018
Intermediate to advanced
346 pages
8h 12m
English
Sometimes referred to as test doubles, stubs are fake implementations of a dependency (that is, an interface) that provides a predictable, usually fixed result. Stubs are also used to help exercise code paths, such as errors, that otherwise might be very difficult or impossible to trigger.
Let's look at an example interface:
type PersonLoader interface { Load(ID int) (*Person, error)}
Let's imagine that the production implementation of the fetcher interface actually calls an upstream REST service. Using our previous types of tests list, we want to test for the following scenarios:
There are ...
Read now
Unlock full access