diff --git a/mock/mock.go b/mock/mock.go index 7530f57..8fe5f27 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -213,7 +213,7 @@ func (m *Mock) On(methodName string, arguments ...interface{}) *Call { // Recording and responding to activity // */ -func (m *Mock) findExpectedCall(method string, arguments ...interface{}) (int, *Call) { +func (m *Mock) FindExpectedCall(method string, arguments ...interface{}) (int, *Call) { m.mutex.Lock() defer m.mutex.Unlock() for i, call := range m.ExpectedCalls { @@ -287,8 +287,11 @@ func (m *Mock) Called(arguments ...interface{}) Arguments { } parts := strings.Split(functionPath, ".") functionName := parts[len(parts)-1] + return m.MethodCalled(functionName, arguments...) +} - found, call := m.findExpectedCall(functionName, arguments...) +func (m *Mock) MethodCalled(functionName string, arguments ...interface{}) Arguments { + found, call := m.FindExpectedCall(functionName, arguments...) if found < 0 { // we have to fail here - because we don't know what to do diff --git a/mock/mock_test.go b/mock/mock_test.go index 16aed51..744ae19 100644 --- a/mock/mock_test.go +++ b/mock/mock_test.go @@ -2,10 +2,11 @@ package mock import ( "errors" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "testing" "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) /* @@ -533,7 +534,7 @@ func Test_Mock_findExpectedCall(t *testing.T) { m.On("Two", 2).Return("two") m.On("Two", 3).Return("three") - f, c := m.findExpectedCall("Two", 3) + f, c := m.FindExpectedCall("Two", 3) if assert.Equal(t, 2, f) { if assert.NotNil(t, c) { @@ -552,7 +553,7 @@ func Test_Mock_findExpectedCall_For_Unknown_Method(t *testing.T) { m.On("Two", 2).Return("two") m.On("Two", 3).Return("three") - f, _ := m.findExpectedCall("Two") + f, _ := m.FindExpectedCall("Two") assert.Equal(t, -1, f) @@ -566,7 +567,7 @@ func Test_Mock_findExpectedCall_Respects_Repeatability(t *testing.T) { m.On("Two", 3).Return("three").Twice() m.On("Two", 3).Return("three").Times(8) - f, c := m.findExpectedCall("Two", 3) + f, c := m.FindExpectedCall("Two", 3) if assert.Equal(t, 2, f) { if assert.NotNil(t, c) { @@ -1156,3 +1157,15 @@ func Test_WaitUntil_Parallel(t *testing.T) { // Allow the first call to execute, so the second one executes afterwards ch2 <- time.Now() } + +func Test_MethodCalled(t *testing.T) { + m := new(Mock) + m.On("foo", "hello").Return("world") + + found, _ := m.FindExpectedCall("foo", "hello") + require.True(t, found >= 0) + retArgs := m.MethodCalled("foo", "hello") + require.True(t, len(retArgs) == 1) + require.Equal(t, "world", retArgs[0]) + m.AssertExpectations(t) +}