mock: in order mock calls

(requested changes applied)
pull/1637/head
Reynier Ortiz 2024-09-06 09:18:07 -04:00
parent bdb1271ed8
commit f17409f81f
2 changed files with 19 additions and 21 deletions

View File

@ -274,6 +274,13 @@ func (c *Call) NotBefore(calls ...*Call) *Call {
}
// InOrder defines the order in which the calls should be made
//
// For example:
//
// InOrder(
// Mock.On("init").Return(nil),
// Mock.On("Do").Return(nil),
// )
func InOrder(calls ...*Call) {
for i := 1; i < len(calls); i++ {
calls[i].NotBefore(calls[i-1])

View File

@ -940,19 +940,15 @@ 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,
mockedService.
On("TheExampleMethod", 1, 2, 3).
Return(4, nil),
mockedService.
On("TheExampleMethod2", true).
Return(),
)
require.Equal(t, []*Call{b, c}, mockedService.ExpectedCalls)
require.NotPanics(t, func() {
mockedService.TheExampleMethod(1, 2, 3)
})
@ -994,20 +990,15 @@ 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,
mockedService.
On("TheExampleMethod", 1, 2, 3).
Return(4, nil).Twice(),
mockedService.
On("TheExampleMethod2", true).
Return(),
)
require.Equal(t, []*Call{b, c}, mockedService.ExpectedCalls)
expectedPanicString := `mock: Unexpected Method Call
-----------------------------