MCPcopy
hub / github.com/stretchr/testify / Unset

Method Unset

mock/mock.go:221–260  ·  view source on GitHub ↗

Unset removes all mock handlers that satisfy the call instance arguments from being called. Only supported on call instances with static input arguments. For example, the only handler remaining after the following would be "MyMethod(2, 2)": Mock. On("MyMethod", 2, 2).Return(0). On("MyMeth

()

Source from the content-addressed store, hash-verified

219// On("MyMethod", Anything, Anything).Return(0)
220// Mock.On("MyMethod", 3, 3).Unset()
221func (c *Call) Unset() *Call {
222 var unlockOnce sync.Once
223
224 for _, arg := range c.Arguments {
225 if v := reflect.ValueOf(arg); v.Kind() == reflect.Func {
226 panic(fmt.Sprintf("cannot use Func in expectations. Use mock.AnythingOfType(\"%T\")", arg))
227 }
228 }
229
230 c.lock()
231 defer unlockOnce.Do(c.unlock)
232
233 foundMatchingCall := false
234
235 // in-place filter slice for calls to be removed - iterate from 0'th to last skipping unnecessary ones
236 var index int // write index
237 for _, call := range c.Parent.ExpectedCalls {
238 if call.Method == c.Method {
239 _, diffCount := call.Arguments.Diff(c.Arguments)
240 if diffCount == 0 {
241 foundMatchingCall = true
242 // Remove from ExpectedCalls - just skip it
243 continue
244 }
245 }
246 c.Parent.ExpectedCalls[index] = call
247 index++
248 }
249 // trim slice up to last copied index
250 c.Parent.ExpectedCalls = c.Parent.ExpectedCalls[:index]
251
252 if !foundMatchingCall {
253 unlockOnce.Do(c.unlock)
254 c.Parent.fail("\n\nmock: Could not find expected call\n-----------------------------\n\n%s\n\n",
255 callString(c.Method, c.Arguments, true),
256 )
257 }
258
259 return c
260}
261
262// NotBefore indicates that the mock should only be called after the referenced
263// calls have been called as expected. The referenced calls may be from the

Calls 4

lockMethod · 0.95
callStringFunction · 0.85
DiffMethod · 0.80
failMethod · 0.45