Documentation ¶
Index ¶
- Variables
- type Allowed
- func (a *Allowed) AndPanic(reason interface{})
- func (a *Allowed) AndReturn(results ...interface{})
- func (a *Allowed) Call(method string, params ...interface{}) *Allowed
- func (a Allowed) Do(doer interface{})
- func (a Allowed) Panic(reason interface{})
- func (a Allowed) Return(results ...interface{})
- func (a *Allowed) ToReceive(method string, params ...interface{}) *Allowed
- func (a *Allowed) With(params ...interface{}) *Allowed
- type Call
- type CallFunc
- type Matcher
- type Mock
- type Spy
Constants ¶
This section is empty.
Variables ¶
var ChooseCall func([]Call) Call
ChooseCall is a tiebreaker when several allowed calls match a given set of parameters. If nil, the default behavior is to choose the most recently allowed call.
Functions ¶
This section is empty.
Types ¶
type Allowed ¶
type Allowed struct {
// contains filtered or unexported fields
}
Allowed is a DSL object that lets you specify parameters, return values and other behaviors for a mocked call. Calls to gomuti.Allow() return this type; each method that you call, refines the mock behavior that you are defining. The behavior is "completed" when you specify an outcome for the mock behavior by calling Return, Panic or Do.
When your mock receives a method call, it is compared to each call that you have allowed for that method name. Gomuti matches the actual parameters against the allowed call's parameter matchers and computes a score for that allowed call, then picks the allowed call with the highest score. If two or more calls have an identical score, the most recently-allowed call wins.
Gomuti uses the following rules to score method calls:
- If the number of actual parameters varies from the allowed, score = 0 (disqualify the allowed call as a match).
- If the allowed call did not specify any parameters, score = 1 (the allowed call matches any number of actual parameters, but just barely).
3) For each parameter that matches a gomega.BeEqual matcher, score += 4.
4) For each parameter that matches a gomega.BeEquivalentTo matcher, score += 3
5) For each parameter that matches another matcher, score += 2
The scoring algorithm sounds complicated, but it results in a very natural- feeling matching behavior. Imagine that we are mocking a method Add(a, b, c interface{}) which adds its parameters:
Â(double, "Add").Panic("so confused") Â(double, "Add").With(1,2,3).Return(6) Â(double, "Add").With(1,2,AnythingOfType("int")).Panic("so number. very count. wow.") Â(double, "Add").With(BeNumerically(">",0),EquivalentTo(2.0),Anything()).Return(0) double.Add(7,2,"hi") // returns 0 double.Add(1,2,99) // panics with a doge message double.Add(1,2,3) // returns 6 double.Add(8,8,8) // panics with a confused message
func (*Allowed) AndPanic ¶
func (a *Allowed) AndPanic(reason interface{})
AndPanic is an alias for Panic()
func (*Allowed) AndReturn ¶
func (a *Allowed) AndReturn(results ...interface{})
AndReturn is an alias for Return()
func (*Allowed) Call ¶
Call allows the mock to receive a method call with matching parameters and return a specific set of values.
There is typically no need to call this method directly since calls to gomuti.Allow() have already set the method name on the Allowed that they return to you.
If you call this method twice on the same Allowed, gomuti panics.
func (Allowed) Do ¶
func (a Allowed) Do(doer interface{})
Do allows you to provide a function that the mock will call in order to determine the correct behavior when a call is matched. You can use it to cause side effects or record parameters of your mock calls.
You can pass any function signature to Do; however, if the signature does not match the signature of the method being mocked, gomuti will cause a panic when you call the mock method. Use this method with care!
If you call this method twice on the same Allowed, gomuti panics.
func (Allowed) Panic ¶
func (a Allowed) Panic(reason interface{})
Panic specifies that the mock should panic with the given reason when a method call is matched. It must be called after Call/ToReceive.
func (Allowed) Return ¶
func (a Allowed) Return(results ...interface{})
Return specifies what the mock should return when a method call is matched. It must be called after Call/ToReceive.
func (*Allowed) With ¶
With allows you to match method parameters of a mock call. You provide a Matcher or a literal value for each position; when someone calls the mock, the call is considered to be matched to this behavior if all parameters match the values you have provided.
If you call this method twice on the same Allowed, gomuti panics.
MATCHING LITERAL PARAMETER VALUES ¶
Literal values of basic type (int, bool, string, etc) are converted to equality matchers; literal pointers and interface types (slice, map, etc) are converted to equivalency matchers. Therefore, calling this:
Â(double, "Foo").With("hello", 42, time.Now())
is the same as calling this:
Â(double, "Foo").With(Equals("hello"), Equals(42), BeEquivalentTo(time.Now()))
MATCHING RANGES OF VALUES ¶
Any Gomega or Gomuti matcher can be used to match a parameter, enabling very sophisticated behavior; for instance:
Â(double, "Foo").With( MatchRegexp("hello|goodbye"), BeNumerically("<=", 42), BeTrue() )
MATCHING VARIADIC PARAMETERS ¶
Variadic parameters are converted to a slice of values and matched against the final matcher provided to With. For instance:
Â(double, "Foo").With(true, []int{1,2,3}) double.Foo(true, 1, 2, 3)
If you call this method twice on the same Allowed, gomuti panics.
type Call ¶
Call represents a method call that has been programmed on a Mock with a call to `gomuti.Allow()`.
type CallFunc ¶
type CallFunc func(...interface{}) []interface{}
CallFunc is a function signature that you can pass to Allow() in order to have fine-gained control over the behavior of your mocks. You are responsible for validating the number and type of parameters, and ensuring that the number of returned values is correct for the mocked method.
type Matcher ¶
Matcher is a method parameter matcher for mock calls. It is a subset of Gomega's Matcher interface, but does not contain any failure-message functions since a failure to match method parameters is not by itself a failure.
func MatchParams ¶
func MatchParams(params []interface{}) []Matcher
MatchParams returns a sequence of gomuti Matchers that will match the specified method-parameter sequence. For parameters that are not already a Matcher, it uses a heuristic to create an equality, equivalency or be-nil matcher. For parameters that are already a matcher, it returns the matcher verbatim.
type Mock ¶
Mock is a state container for mocked behavior. Rather than instantiating it directly, you should include a field of this type in your mock structs and define methods that delegate their behavior to the mock's Call() method.
If all of this sounds like too much work, then you should really check out https://github.com/xeger/mongoose to let the computer generate your mocks for you!
func FindMock ¶
FindMock uses reflection to find the mock-controller associated with a given value. Its behavior varies depending on the type of the value:
- Instance of Mock: return the value itself
- Pointer to Mock: return the pointed-to value
- Struct that contains a Mock field: 3a) if the field is nil, panic (user must initialize the field) 3b) return the field's value
- Pointer to struct that contains a Mock field: 4a) if the field is nil, initialize it to an empty Mock 4b) return the field's value
- Anything else: panic (don't know how to mock behaviors for ...)
func (Mock) Allow ¶
Allow returns an object that can be used to program an expected method call. Rather than calling this directly, you probably want to call gomuti.Allow() on some struct that contains a Mock.
func (Mock) Call ¶
Call informs the mock that a call has been made; if the call matches a call that was programmed with Allow(), it returns non-nil. Methods that return nothing, still return an empty slice if the call was matched.
In contrast, if this method returns nil then the method call was NOT matched and the caller should behave accordingly, i.e. panic unless some stubbed default behavior is appropriate.
type Spy ¶
type Spy map[string][]called
Spy is a state container for recording information about calls made to a test double.
func FindSpy ¶
FindSpy uses reflection to find the spy-controller associated with a given value. Its behavior varies depending on the type of the value:
- Instance of Spy: return the value itself
- Pointer to Spy: return the pointed-to value
- Struct that contains a Spy field: 3a) if the field is nil, panic (user must initialize the field) 3b) return the field's value
- Pointer to struct that contains a Spy field: 4a) if the field is nil, initialize it to an empty Spy 4b) return the field's value
- Anything else: panic (don't know how to spy on ...)
func (Spy) ClosestMatch ¶
ClosestMatch returns the parameters of the recorded call that most closely matches the given criteria, or nil if the method was never called at all.