Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock: in order mock calls #1637

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ func (c *Call) NotBefore(calls ...*Call) *Call {
return c
}

// InOrder defines the order in which the calls should be made
brackendawson marked this conversation as resolved.
Show resolved Hide resolved
func InOrder(calls ...*Call) {
for i := 1; i < len(calls); i++ {
calls[i].NotBefore(calls[i-1])
}
}

// Mock is the workhorse used to track activity on another object.
// For an example of its usage, refer to the "Example Usage" section at the top
// of this document.
Expand Down
59 changes: 59 additions & 0 deletions mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,30 @@ func Test_Mock_Return_NotBefore_In_Order(t *testing.T) {
})
}

func Test_Mock_Return_InOrder_Uses_NotBefore(t *testing.T) {
var mockedService = new(TestExampleImplementation)

b := mockedService.
On("TheExampleMethod", 1, 2, 3).
Return(4, nil)
c := mockedService.
On("TheExampleMethod2", true).
Return()

InOrder(
b,
c,
)

require.Equal(t, []*Call{b, c}, mockedService.ExpectedCalls)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is testing the implementation. A cost effective test would only test the behaviour, as you are doing just fine after this line.

If you remove this assertion then we are free to change the implementation of the intended behaviour without breaking the test. It also removes the need to declare b and c, so the test can be written as InOrder is intended to be used.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it's just comparing pointer values. If InOrder did literally nothing then that assertion would still pass.

require.NotPanics(t, func() {
mockedService.TheExampleMethod(1, 2, 3)
})
require.NotPanics(t, func() {
mockedService.TheExampleMethod2(true)
})
}

func Test_Mock_Return_NotBefore_Out_Of_Order(t *testing.T) {
var mockedService = new(TestExampleImplementation)

Expand Down Expand Up @@ -967,6 +991,40 @@ TheExampleMethod(int,int,int)
})
}

func Test_Mock_Return_InOrder_Uses_NotBefore_Out_Of_Order(t *testing.T) {
var mockedService = new(TestExampleImplementation)

b := mockedService.
On("TheExampleMethod", 1, 2, 3).
Return(4, nil).Twice()
c := mockedService.
On("TheExampleMethod2", true).
Return()

InOrder(
b,
c,
)

require.Equal(t, []*Call{b, c}, mockedService.ExpectedCalls)

expectedPanicString := `mock: Unexpected Method Call
-----------------------------

TheExampleMethod2(bool)
0: true

Must not be called before:

TheExampleMethod(int,int,int)
0: 1
1: 2
2: 3`
require.PanicsWithValue(t, expectedPanicString, func() {
mockedService.TheExampleMethod2(true)
})
}

func Test_Mock_Return_NotBefore_Not_Enough_Times(t *testing.T) {
var mockedService = new(TestExampleImplementation)

Expand Down Expand Up @@ -1022,6 +1080,7 @@ func Test_Mock_Return_NotBefore_Different_Mock_In_Order(t *testing.T) {
mockedService2.TheExampleMethod2(true)
})
}

func Test_Mock_Return_NotBefore_Different_Mock_Out_Of_Order(t *testing.T) {
var (
mockedService1 = new(TestExampleImplementation)
Expand Down