June 2017
Intermediate to advanced
400 pages
10h 32m
English
This recipe demonstrates how to mock interfaces as well as functions that have been declared as variables. There are also certain libraries that can mimic this patch/restore directly on declared functions, but they bypass a lot of Go's type safety to accomplish that feat. If you need to patch functions from an external package, you may use the following trick:
// whatever package you wanna patchimport "github.com/package" // this is patchable using the method described in this recipevar packageDoSomething = package.DoSomething
For this recipe, we start by setting up our test and using table-driven tests. There's a lot of literature about this technique, and I recommend exploring it further. Once our tests are set up, we ...
Read now
Unlock full access