Skip to content

Commit 6241f9a

Browse files
neilisaacboyan-soubachov
authored andcommitted
Add String method to Mock to fix #625
1 parent dc5c261 commit 6241f9a

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

mock/mock.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ type Mock struct {
221221
mutex sync.Mutex
222222
}
223223

224+
// String provides a %v format string for Mock.
225+
// Note: this is used implicitly by Arguments.Diff if a Mock is passed.
226+
// It exists because go's default %v formatting traverses the struct
227+
// without acquiring the mutex, which is detected by go test -race.
228+
func (m *Mock) String() string {
229+
return fmt.Sprintf("%[1]T<%[1]p>", m)
230+
}
231+
224232
// TestData holds any data that might be useful for testing. Testify ignores
225233
// this data completely allowing you to do whatever you like with it.
226234
func (m *Mock) TestData() objx.Map {

mock/mock_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,3 +1653,40 @@ func unexpectedCallRegex(method, calledArg, expectedArg, diff string) string {
16531653
func ConcurrencyTestMethod(m *Mock) {
16541654
m.Called()
16551655
}
1656+
1657+
func TestConcurrentArgumentRead(t *testing.T) {
1658+
methodUnderTest := func(c caller, u user) {
1659+
go u.Use(c)
1660+
c.Call()
1661+
}
1662+
1663+
c := &mockCaller{}
1664+
defer c.AssertExpectations(t)
1665+
1666+
u := &mockUser{}
1667+
defer u.AssertExpectations(t)
1668+
1669+
done := make(chan struct{})
1670+
1671+
c.On("Call").Return().Once()
1672+
u.On("Use", c).Return().Once().Run(func(args Arguments) { close(done) })
1673+
1674+
methodUnderTest(c, u)
1675+
<-done // wait until Use is called or assertions will fail
1676+
}
1677+
1678+
type caller interface {
1679+
Call()
1680+
}
1681+
1682+
type mockCaller struct{ Mock }
1683+
1684+
func (m *mockCaller) Call() { m.Called() }
1685+
1686+
type user interface {
1687+
Use(caller)
1688+
}
1689+
1690+
type mockUser struct{ Mock }
1691+
1692+
func (m *mockUser) Use(c caller) { m.Called(c) }

0 commit comments

Comments
 (0)