Commit Graph

144 Commits (89086b0757c069a4ab49a42f36f8a2e54c831e4b)

Author SHA1 Message Date
Konstantin Cherkasov b72b9c302b Fix typo: arugment -> argument 2016-05-04 09:50:41 +03:00
Ernesto Jiménez c5d7a69bf8 Fixes #300 - Fix backwards-incompatible change 2016-04-18 23:58:27 +01:00
Ernesto Jiménez 3058ec34fb Document chances of false reults on mock asserts 2016-04-12 21:41:11 +01:00
Ernesto Jiménez 096a13e3a4 Fix #293 - Fix mock.AssertExpectations w/ pointers
AssertExpectations did fail using an argument pointer whose underlying
value changed before the assertion.
2016-04-12 21:25:54 +01:00
Will Ogden 17400e7672 Update MatchedBy example to include actual use of MatchedBy! 2016-04-07 11:11:40 +01:00
Thomas LE ROUX dcb39ac4d8 docs: Fix a typo 2016-01-29 17:29:26 +01:00
Ernesto Jiménez 1661650f98 Fix most golint warnings 2016-01-09 19:30:22 +01:00
Ernesto Jiménez 7e30b2ce7a Fixes #261 - Add FailNow back 2016-01-08 12:25:24 +01:00
Ernesto Jiménez 347825ef79 Use Godeps with GO15VENDOREXPERIMENT 2016-01-07 18:08:03 +01:00
Ernesto Jiménez 15b3d4d0fc Merge pull request #249 from stretchr/godep
Vendor dependencies and rewrite import paths
2016-01-07 16:54:51 +01:00
Ernesto Jiménez 5b9da39b66 MatchedBy documentation 2016-01-02 12:02:58 +01:00
Ernesto Jiménez 6905a3e663 Merge pull request #227 from pippio/master
Add custom mock argument matching
2016-01-02 11:55:03 +01:00
Ernesto Jiménez d04dca765f Vendor dependencies and rewrite import paths 2015-12-02 20:32:31 +01:00
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
Ernesto Jiménez ef3ce2dd13 Show struct fields in mock unexpected method error 2015-11-02 00:46:20 +00:00
Levi Corcoran 3c35d25e9b fix race conditions related to mutation of expectedCall.Repeatability 2015-10-07 16:34:11 -05:00
Srini Brahmaroutu 0df687d7a9 Due to function name format differences, code fails with GCCGO
Signed-off-by: Srini Brahmaroutu <srbrahma@us.ibm.com>
2015-09-29 17:07:45 +00:00
Ernesto Jiménez b8dc1cecf1 Merge pull request #208 from tcsc/master
Allows mock call expectations to be defined in arbitrary order
2015-09-14 10:32:55 +01: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
Boris Pruessmann 84e2423404 AssertNumberOfCalls: wrong order of arguments
The assert.Equal called had the actual and expected values mixed up.
2015-07-03 13:30:03 +02:00
Sergiusz Urbaniak b11fb16915 mock: fix races to m.Calls/m.ExpectedCalls
Fixes races inside the mock package caused by
unsychronized reads/writes of m.Calls/m.ExpectedCalls.
2015-06-05 16:24:27 +02:00
Lee Packham 9768526b70 Fix race condition with ExpectedCalls
There is race condition caused when a method being tested calls a mocked
method within a go routine. For example, caching might be done a go
routine and that caching might be mocked.

There is already a mutex protecting the lists on the Mock object -
however Return() appends to ExpectedCalls and findExpectedCall could
run at the same time.
2015-06-02 12:54:01 +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 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
Davide D'Agostino e9fe59ca38 Merge pull request #148 from ernesto-jimenez/master
Mock can block returns with .After and .WaitUntil
2015-04-17 12:07:20 -07:00
lummie 3a9efeedd4 Fix "go vet" errors for incorrect printf verb
github.com/stretchr/testify/mock/mock.go:289: arg m.ExpectedCalls for printf verb %s of wrong type: []mock.Call                                                                                                                       
github.com/stretchr/testify/mock/mock.go:298: arg m.ExpectedCalls for printf verb %s of wrong type: []mock.Call
2015-04-17 16:21:01 +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
Christian Mesh 5d4510b137 Fix mock.go's Int Bool and Error shortcut methods
When using testify, I saw panic: assert: arguments: Error(0) failed because object wasn't correct type: {{ad6e7d77-1745-4761-6c4b-a79ab92a4a0f e6e975ef-3637-4047-436a-018ee9d2de52 %!s(int=17) <nil> <nil> %!s(bool=false) %!s(*string=<nil>) ...
when using the Error(index) function.

This commit simply changes to using %v for printing the unexpected structure in the Bool, Int, and Error shortcut methods.
2014-10-15 14:01:54 -04:00
Anup Chenthamarakshan 27b04ffad0 Fix issue #55 - Serialize calls to Mock.Called. 2014-07-11 22:17:41 -07:00
Pieter Noordhuis 939e66beb2 Fix use of AnythingOfType with AssertCalled 2014-04-01 15:52:39 -07:00
Onsi Fakhouri 59b459277c Define a TestingT interface in assertions.go and mock.go 2014-01-20 18:25:43 -08: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
Tyler Bunnell f3d115a597 Remove unreachable code 2013-06-12 09:35:26 -06:00
Mat Ryer ae878dc5c4 repo name change 2013-06-11 22:44:58 -06:00
Mat Ryer d6863491da fixed bug in String 2013-04-30 10:40:31 -06:00
Mat Ryer 87ce1d3239 added TestData to mocks 2013-04-30 10:35:18 -06:00
Tyler Bunnell 044591a481 go fmt 2013-03-31 09:56:37 -06:00
Tyler Bunnell 85763a0f23 Sprint->Sprintf 2013-03-31 09:42:14 -06:00
Tyler Bunnell cfc83347cf Remove unreachable code 2013-03-31 09:41:59 -06:00
Tyler Bunnell 8014de2adc Fixes #5 2012-10-17 16:22:11 -06:00
Tyler Bunnell 2930d903bf Initial Commit 2012-10-16 11:14:23 -06:00