A mocking framework for Go inspired by Google Mock for C++ and Google JS Test.
Go
Latest commit e94d794 Aug 31, 2015 @jacobsa Support generating output for inclusion in the interface package.
This makes it possible to use mocks from tests in the same package as
the interfaces without dependency cycles.
Failed to load latest commit information.
createmock Support generating output for inclusion in the interface package. Aug 31, 2015
generate Support generating output for inclusion in the interface package. Aug 31, 2015
sample Fixed formatting. Oct 22, 2012
.gitignore Added a gitignore file. Dec 20, 2011
.travis.yml Enabled Travis CI. Jul 20, 2015
LICENSE Added a license file. Dec 20, 2011
README.md Fixed an incorrect link. Feb 17, 2015
action.go Updated test code for SetSignature. Feb 3, 2012
controller.go Fixed formatting. Oct 22, 2012
controller_test.go Fixed formatting. Oct 22, 2012
do_all.go doAll.Invoke Apr 28, 2015
do_all_test.go Fixed test bugs. Apr 28, 2015
doc.go Added a package overview. Aug 31, 2015
error_reporter.go
expectation.go Added other stub methods. Dec 21, 2011
integration_test.go Fixed formatting. Oct 22, 2012
internal_expectation.go Added mutexes to internal_expectation.go. Feb 6, 2012
internal_expectation_test.go Fixed formatting. Oct 22, 2012
invoke.go Fixed a panic. Feb 13, 2012
invoke_test.go
mock_object.go Implemented Oglemock_Description. Dec 21, 2011
return.go Fixed formatting. Oct 22, 2012
return_test.go Fixed formatting. Oct 22, 2012
save_arg.go saveArg.Invoke Apr 28, 2015
save_arg_test.go Fixed a test bug. Apr 28, 2015

README.md

GoDoc

oglemock is a mocking framework for the Go programming language with the following features:

  • An extensive and extensible set of matchers for expressing call expectations (provided by the oglematchers package).

  • Clean, readable output that tells you exactly what you need to know.

  • Style and semantics similar to Google Mock and Google JS Test.

  • Seamless integration with the ogletest unit testing framework.

It can be integrated into any testing framework (including Go's testing package), but out of the box support is built in to ogletest and that is the easiest place to use it.

Installation

First, make sure you have installed Go 1.0.2 or newer. See here for instructions.

Use the following command to install oglemock and its dependencies, and to keep them up to date:

go get -u github.com/jacobsa/oglemock
go get -u github.com/jacobsa/oglemock/createmock

Those commands will install the oglemock package itself, along with the createmock tool that is used to auto-generate mock types.

Generating and using mock types

Automatically generating a mock implementation of an interface is easy. If you want to mock interfaces Bar and Baz from package foo, simply run the following:

createmock foo Bar Baz

That will print source code that can be saved to a file and used in your tests. For example, to create a mock_io package containing mock implementations of io.Reader and io.Writer:

mkdir mock_io
createmock io Reader Writer > mock_io/mock_io.go

The new package will be named mock_io, and contain types called MockReader and MockWriter, which implement io.Reader and io.Writer respectively.

For each generated mock type, there is a corresponding function for creating an instance of that type given a Controller object (see below). For example, to create a mock reader:

someController := [...]  // See next section.
someReader := mock_io.NewMockReader(someController, "Mock file reader")

The snippet above creates a mock io.Reader that reports failures to someController. The reader can subsequently have expectations set up and be passed to your code under test that uses an io.Reader.

Getting ahold of a controller

oglemock.Controller is used to create mock objects, and to set up and verify expectations for them. You can create one by calling NewController with an ErrorReporter, which is the basic type used to interface between oglemock and the testing framework within which it is being used.

If you are using ogletest you don't need to worry about any of this, since the TestInfo struct provided to your test's SetUp function already contains a working Controller that you can use to create mock object, and you can use the built-in ExpectCall function for setting expectations. (See the ogletest documentation for more info.) Otherwise, you will need to implement the simple ErrorReporter interface for your test environment.

Documentation

For thorough documentation, including information on how to set up expectations, see here.