From f17409f81f93ebd964caa7a36793134d68e23fdb Mon Sep 17 00:00:00 2001 From: Reynier Ortiz Date: Fri, 6 Sep 2024 09:18:07 -0400 Subject: [PATCH] mock: in order mock calls (requested changes applied) --- mock/mock.go | 7 +++++++ mock/mock_test.go | 33 ++++++++++++--------------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/mock/mock.go b/mock/mock.go index 6446445..920a87a 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -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]) diff --git a/mock/mock_test.go b/mock/mock_test.go index bc3c5ad..4878b67 100644 --- a/mock/mock_test.go +++ b/mock/mock_test.go @@ -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 -----------------------------