Commit Graph

40 Commits (7538ed3e8963d2b55cc044833328039cd4a1efb2)

Author SHA1 Message Date
Dinesh Kumar 7538ed3e89 displaying mismatch information of arguments diff while panic for closest function call. closes #556 2018-02-21 11:39:36 +05:30
Dinesh Kumar be8372ae8e Adding logging when mock assertions fails 2018-02-06 08:25:39 +00:00
Ruben de Vries 8824eb48ce Store CallerInfo when On() is called and print for missing calls during AssertExpectations. 2018-02-02 18:36:05 +00:00
Dinesh Kumar b3596e9279 Fxinng After(time.Duration) to wait properly
commit 5b0291d47dc3a70cc6c6be3bba3c2f934a8e933e
Merge: 1f324ec 8ccf48a
Author: Dinesh Kumar <dineshkumar-cse@users.noreply.github.com>
Date:   Sat Dec 30 19:55:13 2017 +0530

    Merge branch 'master' into master

commit 1f324ec8cbe892a2c5364904643aa1b710121824
Author: Dinesh Kumar <dinesh.kumar@go-jek.com>
Date:   Sat Dec 30 19:07:16 2017 +0530

    Fixing comments: reduced test time, locking in after, unexported waitTime

    - WaitUntil/After overrides each other value
    - currently if channel is set that takes the priority, if not the
      waitTime is used to Sleep

commit a7101ec14224918fc06532acae76418d1b4f04c5
Author: Dinesh Kumar <dinesh.kumar@go-jek.com>
Date:   Fri Dec 29 13:02:14 2017 +0530

    Using if else instead of switch and pulling out the commone one

commit 936f63dd689acacfd051867c5c700489caa1dbc2
Author: Dinesh Kumar <dinesh.kumar@go-jek.com>
Date:   Fri Dec 29 12:41:40 2017 +0530

    After - making it wait during method call

    After was using call.WaitUntil(time.After(duration)),
    <-time.After(duration) returns a channel with the timer immediately
    started
2017-12-30 17:53:34 +01:00
Joel Low cbf22d8d28 Support nil arguments to Argument Matchers 2017-12-30 17:48:55 +01:00
Peter Hsu 8ccf48a064 Allow a method call to be optional 2017-12-30 15:23:32 +01:00
Dan Heller 2aa2c176b9 Fix unprotected call fields access in MethodCalled()
This change fixes a race condition I discovered when a multithreaded
test in a service I work on failed under -race. The included test case
simulates that failure (concurrent mutation of a Call with invocations
on the mock). The test will fail with a data race if run under the
race detector; the new locking ensures that call fields are not
accessed without the protection of the parent mutex.
2017-10-17 22:22:57 -07:00
Yimin Chen e964b172ca add MethodCalled to the mock
remove IsCallExpected method

add comment
2017-05-30 21:11:52 +01:00
Ernesto Jiménez 345898142b Run go vet in travis 2017-05-29 13:40:41 +01:00
Ernesto Jiménez f712be9266 Revert "add mock.MethodCalled(methodName, args...)"
This reverts commit 17a0bd50e9.

Should avoid simply exporting an internal method.
2017-05-26 07:47:23 +01:00
Ernesto Jiménez 2b76a9702e Revert "Added goconvey style assertions to the mock package"
This reverts commit faf0710ff2.
2017-05-26 07:46:02 +01:00
Richard Knop 18cfa6871f Added extra unit test for function with mixed variadic arguments. 2017-05-25 17:38:37 -07:00
Andy McCall faf0710ff2 Added goconvey style assertions to the mock package 2017-05-25 17:05:23 -07:00
Yimin Chen 17a0bd50e9 add mock.MethodCalled(methodName, args...) 2017-05-25 17:03:17 -07:00
Ernesto Jiménez 598ab58e60 Merge pull request #171 from ANPez/master
Release lock before .WaitUntil
2016-12-17 13:32:40 -06:00
Ernesto Jiménez 92787b8c71 Fixes #342 - AssertExpectationsForObjects takes *Mock 2016-09-24 18:04:24 +01:00
Ernesto Jiménez c5d7a69bf8 Fixes #300 - Fix backwards-incompatible change 2016-04-18 23:58:27 +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
Ernesto Jiménez 1661650f98 Fix most golint warnings 2016-01-09 19:30:22 +01:00
Antonio Nicolás Pina 384cc6617a Merge branch 'master' of https://github.com/stretchr/testify 2015-11-20 13:19:13 +02: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
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
Antonio Nicolás Pina 4a6e51647d Added test to avoid regresions of #167. 2015-05-20 10:14:16 +03: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