November 2018
Intermediate to advanced
346 pages
8h 12m
English
Mocks are very much like stubs, but they have one fundamental difference. Mocks have expectations. When we used stubs, our tests did nothing to validate our usage of the dependency; with mocks, they will. Which you use depends very much on the type of test and the dependency itself. For example, you might want to use a stub for a logging dependency, unless you are writing a test that ensures the code logs in a specific situation. However, you will often need a mock for a database dependency. Let's change our previous tests from stubs to mocks to ensure that we make those calls:
func TestLoadPersonName(t *testing.T) { // this value does not matter as the stub ignores it fakeID := 1 scenarios := []struct { desc string configureMock ...
Read now
Unlock full access