mirror of https://github.com/stretchr/testify.git
parent
46b3c8225b
commit
e964b172ca
17
mock/mock.go
17
mock/mock.go
|
@ -285,9 +285,16 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
|
||||||
}
|
}
|
||||||
parts := strings.Split(functionPath, ".")
|
parts := strings.Split(functionPath, ".")
|
||||||
functionName := parts[len(parts)-1]
|
functionName := parts[len(parts)-1]
|
||||||
|
return m.MethodCalled(functionName, arguments...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MethodCalled tells the mock object that the given method has been called, and gets
|
||||||
|
// an array of arguments to return. Panics if the call is unexpected (i.e. not preceded
|
||||||
|
// by appropriate .On .Return() calls)
|
||||||
|
// If Call.WaitFor is set, blocks until the channel is closed or receives a message.
|
||||||
|
func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Arguments {
|
||||||
m.mutex.Lock()
|
m.mutex.Lock()
|
||||||
found, call := m.findExpectedCall(functionName, arguments...)
|
found, call := m.findExpectedCall(methodName, arguments...)
|
||||||
|
|
||||||
if found < 0 {
|
if found < 0 {
|
||||||
// we have to fail here - because we don't know what to do
|
// we have to fail here - because we don't know what to do
|
||||||
|
@ -297,13 +304,13 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
|
||||||
// b) the arguments are not what was expected, or
|
// b) the arguments are not what was expected, or
|
||||||
// c) the developer has forgotten to add an accompanying On...Return pair.
|
// c) the developer has forgotten to add an accompanying On...Return pair.
|
||||||
|
|
||||||
closestFound, closestCall := m.findClosestCall(functionName, arguments...)
|
closestFound, closestCall := m.findClosestCall(methodName, arguments...)
|
||||||
m.mutex.Unlock()
|
m.mutex.Unlock()
|
||||||
|
|
||||||
if closestFound {
|
if closestFound {
|
||||||
panic(fmt.Sprintf("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n\n%s\n", callString(functionName, arguments, true), callString(functionName, closestCall.Arguments, true), diffArguments(arguments, closestCall.Arguments)))
|
panic(fmt.Sprintf("\n\nmock: Unexpected Method Call\n-----------------------------\n\n%s\n\nThe closest call I have is: \n\n%s\n\n%s\n", callString(methodName, arguments, true), callString(methodName, closestCall.Arguments, true), diffArguments(arguments, closestCall.Arguments)))
|
||||||
} else {
|
} else {
|
||||||
panic(fmt.Sprintf("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(\"%s\").Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", functionName, functionName, callString(functionName, arguments, true), assert.CallerInfo()))
|
panic(fmt.Sprintf("\nassert: mock: I don't know what to return because the method call was unexpected.\n\tEither do Mock.On(\"%s\").Return(...) first, or remove the %s() call.\n\tThis method was unexpected:\n\t\t%s\n\tat: %s", methodName, methodName, callString(methodName, arguments, true), assert.CallerInfo()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,7 +328,7 @@ func (m *Mock) Called(arguments ...interface{}) Arguments {
|
||||||
}
|
}
|
||||||
|
|
||||||
// add the call
|
// add the call
|
||||||
m.Calls = append(m.Calls, *newCall(m, functionName, arguments...))
|
m.Calls = append(m.Calls, *newCall(m, methodName, arguments...))
|
||||||
m.mutex.Unlock()
|
m.mutex.Unlock()
|
||||||
|
|
||||||
// block if specified
|
// block if specified
|
||||||
|
|
|
@ -2,10 +2,11 @@ package mock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1184,3 +1185,13 @@ func Test_WaitUntil_Parallel(t *testing.T) {
|
||||||
// Allow the first call to execute, so the second one executes afterwards
|
// Allow the first call to execute, so the second one executes afterwards
|
||||||
ch2 <- time.Now()
|
ch2 <- time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_MockMethodCalled(t *testing.T) {
|
||||||
|
m := new(Mock)
|
||||||
|
m.On("foo", "hello").Return("world")
|
||||||
|
|
||||||
|
retArgs := m.MethodCalled("foo", "hello")
|
||||||
|
require.True(t, len(retArgs) == 1)
|
||||||
|
require.Equal(t, "world", retArgs[0])
|
||||||
|
m.AssertExpectations(t)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue