Using the Mockgen package
The previous example used our custom mock objects. When you're working with a lot of interfaces, writing these can become cumbersome and error prone. This is a place where generating code makes a lot of sense. Fortunately, there's a package called github.com/golang/mock/gomock
that provides a generation of mock objects and gives us a very useful library to use in conjunction with interface testing.
This recipe will explore some of the functionality of gomock
and will cover trade-offs on where, when, and how to work with and generate mock objects.
Getting ready
Configure your environment according to these steps:
- Refer to the Getting ready section of theMocking using the standard library recipe of this chapter.
- Run the
go get github.com/golang/mock/
command.
How to do it...
These steps cover writing and running your application:
- From your terminal/console application, create the
chapter8/mockgen
directory and navigate to it. - Copy tests from https://github.com/agtorre/go-cookbook...