Skip to content

Commit 35d4bf5

Browse files
sarathsp06boyan-soubachov
authored andcommitted
panic: add .Panic() to mock.Call
Panic mocks a function call that panics with th specfied message
1 parent 2566b66 commit 35d4bf5

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

mock/mock.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ type Call struct {
6565
// reference. It's useful when mocking methods such as unmarshalers or
6666
// decoders.
6767
RunFn func(Arguments)
68+
69+
// PanicMsg holds msg to be used to mock panic on the function call
70+
// if the PanicMsg is set to a non nil string the function call will panic
71+
// irrespective of other settings
72+
PanicMsg *string
6873
}
6974

7075
func newCall(parent *Mock, methodName string, callerInfo []string, methodArguments ...interface{}) *Call {
@@ -77,6 +82,7 @@ func newCall(parent *Mock, methodName string, callerInfo []string, methodArgumen
7782
Repeatability: 0,
7883
WaitFor: nil,
7984
RunFn: nil,
85+
PanicMsg: nil,
8086
}
8187
}
8288

@@ -100,6 +106,18 @@ func (c *Call) Return(returnArguments ...interface{}) *Call {
100106
return c
101107
}
102108

109+
// Panic specifies if the functon call should fail and the panic message
110+
//
111+
// Mock.On("DoSomething").Panic("test panic")
112+
func (c *Call) Panic(msg string) *Call {
113+
c.lock()
114+
defer c.unlock()
115+
116+
c.PanicMsg = &msg
117+
118+
return c
119+
}
120+
103121
// Once indicates that that the mock should only return the value once.
104122
//
105123
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Once()
@@ -392,6 +410,13 @@ func (m *Mock) MethodCalled(methodName string, arguments ...interface{}) Argumen
392410
time.Sleep(call.waitTime)
393411
}
394412

413+
m.mutex.Lock()
414+
panicMsg := call.PanicMsg
415+
m.mutex.Unlock()
416+
if panicMsg != nil {
417+
panic(*panicMsg)
418+
}
419+
395420
m.mutex.Lock()
396421
runFn := call.RunFn
397422
m.mutex.Unlock()

mock/mock_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,29 @@ func Test_Mock_Return(t *testing.T) {
486486
assert.Nil(t, call.WaitFor)
487487
}
488488

489+
func Test_Mock_Panic(t *testing.T) {
490+
491+
// make a test impl object
492+
var mockedService = new(TestExampleImplementation)
493+
494+
c := mockedService.
495+
On("TheExampleMethod", "A", "B", true).
496+
Panic("panic message for example method")
497+
498+
require.Equal(t, []*Call{c}, mockedService.ExpectedCalls)
499+
500+
call := mockedService.ExpectedCalls[0]
501+
502+
assert.Equal(t, "TheExampleMethod", call.Method)
503+
assert.Equal(t, "A", call.Arguments[0])
504+
assert.Equal(t, "B", call.Arguments[1])
505+
assert.Equal(t, true, call.Arguments[2])
506+
assert.Equal(t, 0, call.Repeatability)
507+
assert.Equal(t, 0, call.Repeatability)
508+
assert.Equal(t, "panic message for example method", *call.PanicMsg)
509+
assert.Nil(t, call.WaitFor)
510+
}
511+
489512
func Test_Mock_Return_WaitUntil(t *testing.T) {
490513

491514
// make a test impl object
@@ -1420,6 +1443,14 @@ func Test_MockMethodCalled(t *testing.T) {
14201443
m.AssertExpectations(t)
14211444
}
14221445

1446+
func Test_MockMethodCalled_Panic(t *testing.T) {
1447+
m := new(Mock)
1448+
m.On("foo", "hello").Panic("world panics")
1449+
1450+
require.PanicsWithValue(t, "world panics", func() { m.MethodCalled("foo", "hello") })
1451+
m.AssertExpectations(t)
1452+
}
1453+
14231454
// Test to validate fix for racy concurrent call access in MethodCalled()
14241455
func Test_MockReturnAndCalledConcurrent(t *testing.T) {
14251456
iterations := 1000

0 commit comments

Comments
 (0)