Skip to content

Commit f6aceb7

Browse files
committed
mock: clear call from NotBefore expectations on Unset()
1 parent 632a260 commit f6aceb7

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

Diff for: mock/mock.go

+20
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ type Call struct {
7878

7979
// Calls which must be satisfied before this call can be
8080
requires []*Call
81+
82+
// Calls that depend on this call to be satisfied so succeed
83+
requiredBy []*Call
8184
}
8285

8386
func newCall(parent *Mock, methodName string, callerInfo []string, methodArguments ...interface{}) *Call {
@@ -242,6 +245,21 @@ func (c *Call) Unset() *Call {
242245
// trim slice up to last copied index
243246
c.Parent.ExpectedCalls = c.Parent.ExpectedCalls[:index]
244247

248+
// in-place filter slice for dependent calls to be cleaned - iterate from 0'th to last skipping unnecessary ones
249+
for _, dependentCall := range c.requiredBy {
250+
var index int
251+
for _, requiredByDependent := range dependentCall.requires {
252+
if requiredByDependent == c {
253+
// Remove from the required calls of the dependent call
254+
continue
255+
}
256+
dependentCall.requires[index] = requiredByDependent
257+
index++
258+
}
259+
dependentCall.requires = dependentCall.requires[:index]
260+
}
261+
c.requiredBy = []*Call{}
262+
245263
if !foundMatchingCall {
246264
unlockOnce.Do(c.unlock)
247265
c.Parent.fail("\n\nmock: Could not find expected call\n-----------------------------\n\n%s\n\n",
@@ -267,6 +285,8 @@ func (c *Call) NotBefore(calls ...*Call) *Call {
267285
if call.Parent == nil {
268286
panic("not before calls must be created with Mock.On()")
269287
}
288+
289+
call.requiredBy = append(call.requiredBy, c)
270290
}
271291

272292
c.requires = append(c.requires, calls...)

Diff for: mock/mock_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,41 @@ func Test_Mock_UnsetIfAlreadyUnsetFails(t *testing.T) {
592592
assert.Equal(t, 0, len(mockedService.ExpectedCalls))
593593
}
594594

595+
func Test_Mock_UnsetOfCallRequiredByNotBefore(t *testing.T) {
596+
// make a test impl object
597+
var mockedServiceA = new(TestExampleImplementation)
598+
var mockedServiceB = new(TestExampleImplementation)
599+
var mockedServiceC = new(TestExampleImplementation)
600+
601+
mock1 := mockedServiceA.
602+
On("TheExampleMethod", 1, 1, 1).
603+
Return(1).
604+
Once()
605+
606+
mock2 := mockedServiceB.
607+
On("TheExampleMethod", 2, 2, 2).
608+
Return(2).
609+
NotBefore(mock1)
610+
611+
mock3 := mockedServiceC.
612+
On("TheExampleMethod", 3, 3, 3).
613+
Return(3).
614+
NotBefore(mock1).
615+
NotBefore(mock2)
616+
617+
assert.Equal(t, 2, len(mock1.requiredBy))
618+
assert.Equal(t, 1, len(mock2.requires))
619+
assert.Equal(t, 1, len(mock2.requiredBy))
620+
assert.Equal(t, 2, len(mock3.requires))
621+
622+
mock1.Unset()
623+
624+
assert.Equal(t, 0, len(mock1.requiredBy))
625+
assert.Equal(t, 0, len(mock2.requires))
626+
assert.Equal(t, 1, len(mock2.requiredBy))
627+
assert.Equal(t, 1, len(mock3.requires))
628+
}
629+
595630
func Test_Mock_UnsetByOnMethodSpec(t *testing.T) {
596631
// make a test impl object
597632
var mockedService = new(TestExampleImplementation)

Diff for: tests/test_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package kata_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/mock"
7+
)
8+
9+
type myMock struct {
10+
mock.Mock
11+
}
12+
13+
func (m *myMock) Do() {
14+
m.Called()
15+
}
16+
17+
func (m *myMock) Dont() {
18+
m.Called()
19+
}
20+
21+
func TestIfy(t *testing.T) {
22+
m := myMock{}
23+
m.Test(t)
24+
defer m.AssertExpectations(t)
25+
call1 := m.On("Dont").Return().Once()
26+
m.On("Do").Return().Once().NotBefore(call1)
27+
call1.Unset()
28+
29+
m.Do()
30+
}

0 commit comments

Comments
 (0)