Mongoose is a tool that parses your Go source code and generates a mock implementation of every interface it finds. Mongoose can generate code for a number of mocking packages:
Use go get
to place the mongoose
binary under $GOPATH/bin
:
$ go get github.com/xeger/mongoose
Run mongoose
and pass it the name of one or more directories that contain Go sources. If you are working on package example/mockitup
:
$ cd ~/go/src/example/mockitup
$ mongoose .
By default, mocks use the Gomuti package to record and play back method calls. For information on Testify and other supported mocking toolkits, skip to Alternative mocking packages below.
Use the -r
flag to recurse into all subpackages and generate mocks in parallel.
$ cd ~/go/src/gigantic/project
$ mongoose -r commerce services util
Consult the Gomuti documentation for extensive examples. As a cheat sheet:
import (
. "github.com/onsi/ginkgo"
. "github.com/xeger/gomuti"
)
var _ = Describe("stuff", func() {
It("works", func() {
// mongoose has generated this type with one method:
// Add(l, r int64) int64
adder := MockAdder{}
Allow(adder).Call("Add").With(5,5).Return(10)
Allow(adder).Call("Add").With(10,5).Return(15)
Allow(adder).Call("Add").With(BeNumerically(">", 2**31-1),Anything()).Panic()
result := subject.Multiply(3,5))
Expect(adder).To(HaveCall("Add").Times(2))
Expect(result).To(Equal(15))
Expect(func() {
subject.Multiply(2**32, 2)
}).To(Panic())
})
})
TODO - testify docs
$ mongoose -mock=testify somepkg
Mongoose follows the filesystem conventions of testify's mockery tool; each mock is placed in its own file in the same directory as the mocked interface.
WARNING: not implemented yet.
Mongoose could be capable of generating standalone stubs in the style of counterfeiter: https://github.com/maxbrunsfeld/counterfeiter
It would also be nice if Mongoose's standalones could function as stubs.
TODO - docs
Open an issue and explain your problem, steps to reproduce, and your ideal solution (if known).
Fork the xeger/mongoose
repository on GitHub; make your changes; open a pull request.