Lightweight mocking library for function calls in Go
The purpose of this library is to allow easy mocking of function calls, and ease dependency injection.
go get github.com/ebabani/emock
http://godoc.org/github.com/ebabani/emock
The following example uses ginkgo and gomega for assertions
myFunc = func(a int, b string) string {
return fmt.Sprintf("A IS %+v B is %+v", a, b)
}
It("should return an empty string", func() {
mockObj = MockFunc(&myFunc) // (1)
defer mockObj.Restore() // (2)
ret := myFunc(123, "Cool stuff") // (3)
Expect(mockObj.GetArgsForCall(0)).To(MatchArgs(123, "Cool stuff")) // (4)
Expect(mockObj.CallCount()).To(Equal(1)) // (5)
Expect(ret).To(BeEmpty()) // (6)
})
Mock a function at the given address, and return a mock object. The mock object can be used to check how many times the mocked function was called, and with what arguments. (See lines 4 and 5)
Restore the function to its normal behaviour instead of the mock implemetation.
When mocked, by default the function will return zero values, but you can also set custom return values.
GetArgsForCall(i) will return the args used in the i'th call to the mocked function. The args are returned as an []interface{}
MatchArgs is a custom gomega matcher to make it easier to check if the function was called with the right args.
How many times the mocked function was called.
By default when something is mocked it will return zero values for all its returns. (See https://golang.org/ref/spec#The_zero_value)
You can use mockObj.SetReturns
to set custom returns, or mockObj.SetReturnFunc
to replace the mocked function with another one with the same signature.