-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from go-andiamo/spy-mock
Add spy mock
- Loading branch information
Showing
10 changed files
with
354 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/go-andiamo/mmock" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestSpyMockedThingy_MethodNotMockedCallsUnderlying(t *testing.T) { | ||
// call a method that hasn't been set-up... | ||
underlying := &ActualThing{} | ||
spy := mmock.NewSpyMockOf[MockThing, Thingy](underlying) | ||
err := spy.DoSomething() | ||
assert.NoError(t, err) | ||
assert.Equal(t, 1, underlying.calls) | ||
spy.AssertMethodCalled(t, spy.DoSomething) | ||
spy.AssertNumberOfMethodCalls(t, spy.DoSomething, 1) | ||
err = spy.DoSomething() | ||
assert.Error(t, err) | ||
assert.Equal(t, 2, underlying.calls) | ||
spy.AssertMethodCalled(t, spy.DoSomething) | ||
spy.AssertNumberOfMethodCalls(t, spy.DoSomething, 2) | ||
} | ||
|
||
func TestSpyMockedThingy_MethodMocked(t *testing.T) { | ||
// call a method that has been set-up... | ||
underlying := &ActualThing{} | ||
spy := mmock.NewSpyMockOf[MockThing, Thingy](underlying) | ||
spy.OnMethod(spy.DoSomething).Return(nil) | ||
err := spy.DoSomething() | ||
assert.NoError(t, err) | ||
assert.Equal(t, 0, underlying.calls) | ||
spy.AssertMethodCalled(t, spy.DoSomething) | ||
spy.AssertNumberOfMethodCalls(t, spy.DoSomething, 1) | ||
err = spy.DoSomething() | ||
assert.NoError(t, err) | ||
assert.Equal(t, 0, underlying.calls) | ||
spy.AssertMethodCalled(t, spy.DoSomething) | ||
spy.AssertNumberOfMethodCalls(t, spy.DoSomething, 2) | ||
} | ||
|
||
type MockThing struct { | ||
mmock.MockMethods | ||
} | ||
|
||
func (m *MockThing) DoSomething() error { | ||
args := m.Called() | ||
return args.Error(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import "errors" | ||
|
||
type Thingy interface { | ||
DoSomething() error | ||
} | ||
|
||
var _ Thingy = &ActualThing{} | ||
|
||
type ActualThing struct { | ||
calls int | ||
} | ||
|
||
func (a *ActualThing) DoSomething() error { | ||
a.calls++ | ||
if a.calls > 1 { | ||
return errors.New("can't do something more than once") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package mmock | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
// NewSpyMockOf creates a new spy mock of a specified type and provides the underling wrapped implementation | ||
// | ||
// The wrapped arg is implementation to be spied on - any methods that are called on the mock | ||
// but have not been expected (by using On or OnMethod) will call this underlying - but you can still assert | ||
// that the method has been called | ||
func NewSpyMockOf[T any, I any](wrapped I) *T { | ||
r := NewMock[T]() | ||
if _, ok := interface{}(r).(I); !ok { | ||
i := new(I) | ||
panic(fmt.Sprintf("type '%s' does not implement interface '%s'", reflect.TypeOf(r).Elem().String(), reflect.TypeOf(i).Elem().Name())) | ||
} | ||
setSpyOf(r, wrapped) | ||
return r | ||
} | ||
|
||
func setSpyOf(mocked any, wrapped any) { | ||
if msu, ok := mocked.(mockSetup); ok { | ||
msu.SetSpyOf(wrapped) | ||
} | ||
} |
Oops, something went wrong.