Commit Graph

19 Commits (c92828f29518bc633893affbce12904ba41a7cfa)

Author SHA1 Message Date
Johnny Graettinger ed275850b6 Add custom mock argument matching
Add an argumentMatcher type and MatchedBy() helper to the mock
package, enabling custom fine-grained argument matching behaviors.

This is particularly helpful when building mocks over complex argument
types where deep equality is cumbersome or can't be used. Ex, a matcher
might match over just the the URL of an argument *http.Request.
2015-11-09 18:11:46 -05:00
Trent Clarke 49804a382a Allows mock call expectations to be defined in arbitrary order
Originally the expctations required that the return values be specified
immediately after the method name and arguments, otherwise the call
setup will either panic (best case) or silently modify the *previous*
call specification (worst case).

This change moves the Return(), Run(), Once(), etc methods onto the Call
struct, and changes the chaining behaviour so that they modify the Call
data directly rather than referencing the last item in the ExpectedCalls
array.
2015-08-27 22:19:12 +10:00
Ernesto Jiménez 4464dda05e Add extra tests mocking variadic functions 2015-06-02 20:23:13 +01:00
Ernesto Jiménez d6265bda1a Add test mocking variadic function 2015-06-02 19:59:43 +01:00
Ernesto Jiménez 1faea58ff8 Address #159: Meaningful panic when expecting Func
We cannot compare two Func arguments to check if they are equal using
the reflect package. This leads to the following misleading panic:

```go
handler := func(http.ResponseWriter, *http.Request) {}
muxMock.On("HandleFunc", "/", handler)
muxMock.HandleFunc("/", handler)
// panics arguing handler != handler
```

Since we cannot fix this behaviour using reflection, this patch
provides a better panic with a meaningful workaround.

```go
handler := func(http.ResponseWriter, *http.Request) {}
muxMock.On("HandleFunc", "/", handler)
// panics recommending defining the expectatin with:
//   AnythingOfType("func(http.ResponseWriter, *http.Request)")
```

Solution following the panic's instructions:

```go
handler := func(http.ResponseWriter, *http.Request) {}
muxMock.On("HandleFunc", "/",
  mock.AnythingOfType("func(http.ResponseWriter, *http.Request)"))
muxMock.HandleFunc("/", handler)
```
2015-05-06 19:35:54 +01:00
Ernesto Jiménez 8bfd855592 Add test cases mocking methods with Func arguments 2015-05-06 19:24:54 +01:00
Ernesto Jiménez a369d6f598 Allow mocking methods updating referenced structs
Some methods in Go take a pointer to a struct or a map and are supposed
to set some values in the referenced argument. Using Return is not
enough to mock such methods.

We are introducing a Run method that allows setting a Call handler to
make your mock update such referenced values.

Example usage mocking a service that finds a user in a data store and
fills the values in the provided struct:

```go
m.On("Find", 1, mock.AnythingOfType(*User))
  .Return(nil)
  .Run(func(args mock.Arguments) {
  u := args.Get(0).(*User)
  u.ID = 1
  u.Email = "mail@example.com"
})
```
2015-05-04 16:56:39 +01:00
Ernesto Jiménez 0792dedcd1 Mock can block returns with .After and .WaitUntil
You sometimes want to test concurrent behaviour like timeouts, but it
was not currently possible with our mocks.

This commits adds the following functions to Mock:

`After` will block m.Called for the given duration before returning.

```golang
func (m *Mock) After(d time.Duration) *Mock

Mock.On("MyMethod", arg1).After(1 * time.Millisecond)
```

`WaitUntil` is the primitive under `After`, and it's exposed in case you
want somebody wants more control over the returns. `Called` blocks until the w
channel is closed or receives a message.

```golang
func (m *Mock) WaitUntil(w <-chan time.Time) *Mock

Mock.On("MyMethod", arg1).WaitUntil(time.After(1 * time.Millisecond))
```
2015-04-07 23:42:31 +01:00
Anup Chenthamarakshan cb7b6694dc Use mockedObj.Method(args) instead of mockedObj.Mock.Method(args).
These method calls are equivalent, but the former is more concise.

Fixes #95
2014-11-02 14:19:46 -08:00
Pieter Noordhuis 939e66beb2 Fix use of AnythingOfType with AssertCalled 2014-04-01 15:52:39 -07:00
Rafael Garcia b222794f02 failing test for #31 2014-02-05 15:37:34 -08:00
Mat Ryer df4e46959f added test to prove different args 2014-01-28 09:56:42 -07:00
Mariano Cano 84b345c662 Fix support for AnythingOfTypeArgument. 2013-12-05 10:18:56 -08:00
Curtis Schlak ab70c9a56c Now expectations for method calls abides the repeatability argument. 2013-10-04 23:19:54 -05:00
Curtis Schlak 40e4720477 Added repeatability to On/Return expectations. 2013-10-04 23:15:50 -05:00
Tyler Bunnell 855b03c899 objx transition complete 2013-09-17 16:17:34 -06:00
Mat Ryer ae878dc5c4 repo name change 2013-06-11 22:44:58 -06:00
Mat Ryer 87ce1d3239 added TestData to mocks 2013-04-30 10:35:18 -06:00
Tyler Bunnell 2930d903bf Initial Commit 2012-10-16 11:14:23 -06:00