Documentation ¶
Overview ¶
Package gomuti provides a DSL that makes it easy to create test doubles (mocks, spies and stubs) for Golang interfaces. The DSL consists of three components:
1) Allow: a mocking method that records behavior for test doubles.
2) HaveCall: a spying method that verifies test doubles were called in an expected way.
3) Anything, AnythingOfType: parameter matchers used with mocking and spying methods. Gomega matchers can be used as Gomuti parameter matchers: BeNumerically, HaveOccurred, etc.
All of these methods rely on the Mock and Spy types exported by package gomuti/types; test doubles are generally struct types that contain exported fields of type Mock and Spy. The DSL operates on pointers to these structs and uses reflection to access their fields.
The DSL methods accept struct values as well as pointer-to-struct; the benefit of passing pointers is that the nested Mock or Spy will be allocated as needed with no intervention by the caller.
Stubbing is provided by the mongoose package (https://github.com/xeger/mongoose), which also generates Gomuti-compatible mock code for any interface. Stubbed methods are called whenever no mock expectations match a method call; the return value(s) from a stubbed method call are always zero values. Stubbing must be enabled on a per-object basis by setting the Stub field to true.
Index ¶
- func Allow(double interface{}) *types.Allowed
- func Anything() types.GomegaMatcher
- func AnythingOfType(name string) types.GomegaMatcher
- func BeAnything() types.GomegaMatcher
- func HaveCall(method string) *matchers.HaveCallMatcher
- func HaveReceived(method string) *matchers.HaveCallMatcher
- func HaveType(name string) types.GomegaMatcher
- func Â(double interface{}, methodAndParams ...interface{}) *types.Allowed
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Allow ¶
Allow is a mocking method. It accepts a test double and returns a DSL-context object whose methods allow you to specify the test double's behavior when its methods are called.
For information about parameter matching and return values, see types.Allowed.
func Anything ¶
func Anything() types.GomegaMatcher
Anything is an alias for BeAnything, designed to be more readable in the context of a mocked method call.
Example:
Allow(boat).Call("Sail").With("west", Anything(), "mi").Panic("Please use kilometers")
Which makes more sense than "with be-anything."
func AnythingOfType ¶
func AnythingOfType(name string) types.GomegaMatcher
AnythingOfType is an alias for HaveType, designed to be more readable in the context of a mocked method call.
Example:
Allow(myMock).ToReceive("Foo").With(AnythingOfType("mypkg.Widget"))
Which makes more sense than "with have-type."
func BeAnything ¶
func BeAnything() types.GomegaMatcher
BeAnything creates a matcher that is always satisfied. It is useful when you want to mock a method call but don't care about the parameter in a given position.
Example:
Allow(boat).Call("Sail").With("west", BeAnything(), "mi").Panic("Please use kilometers")
func HaveCall ¶
func HaveCall(method string) *matchers.HaveCallMatcher
HaveCall is a spy method. It returns a matcher to verify that a method call was recorded by a spy. You can add more verifications (of parameter values, call count, etc) by calling methods on the returned matcher.
Example:
Expect(double).To(HaveCall("Bar").With(true, 42).Twice())
func HaveReceived ¶
func HaveReceived(method string) *matchers.HaveCallMatcher
HaveReceived is an alias for HaveCall().
func HaveType ¶
func HaveType(name string) types.GomegaMatcher
HaveType creates a matcher that is satisfied by any value whose type matches the specified name. Example:
Expect(4).To(HaveType("int"))
Named types must be prefixed with the name of the package in which they are DEFINED (i.e. with the name that appears in the package statement of the source file where they are defined) and not with the import name that is used to REFER to them. Example:
import banana "time" Expect(banana.Now()).To(HaveType("time.Time"))
func  ¶
 is a mocking method that is an alias for Allow. Use Shift+Option+M to type this symbol on Mac; Alt+0194 on Windows.
As an additional shortcut, Â accepts the mocked method name and parameters as variadic parameters. The caller is still responsible for completing the behavior by calling Return or Panic() on the returned object.
Examples:
Â(double).Call("Foo").With(1,1).Return(2) // no shortcuts Â(double, "Foo").With(2,1).Return(3) // shortcut call Â(double, "Foo",3,1).Return(4) // shortcut params
Types ¶
This section is empty.