stretchr/testify · error
not before calls must be created with Mock.On()
Error message
not before calls must be created with Mock.On()
What it means
Panicked by Call.NotBefore() (mock/mock.go:269) when any of the passed *Call values has a nil Parent field. A Call's Parent is set inside newCall() during Mock.On(); a nil Parent means the Call was constructed manually (e.g. &mock.Call{}) rather than through Mock.On, so testify cannot link ordering constraints to a mock instance.
Source
Thrown at mock/mock.go:275
}
return c
}
// NotBefore indicates that the mock should only be called after the referenced
// calls have been called as expected. The referenced calls may be from the
// same mock instance and/or other mock instances.
//
// Mock.On("Do").Return(nil).NotBefore(
// Mock.On("Init").Return(nil)
// )
func (c *Call) NotBefore(calls ...*Call) *Call {
c.lock()
defer c.unlock()
for _, call := range calls {
if call.Parent == nil {
panic("not before calls must be created with Mock.On()")
}
}
c.requires = append(c.requires, calls...)
return c
}
// 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])View on GitHub (pinned to 001eb7946b)
Solutions
- Ensure every Call passed to NotBefore is the return value of a real Mock.On(...) invocation.
- Replace manual *mock.Call{} literals with m.On(method).Return(...) calls.
- Use mock.InOrder(...) which constructs and links calls correctly for sequential ordering.
Example fix
// before
c := &mock.Call{Method: "Init"}
m.On("Do").Return(nil).NotBefore(c) // panic
// after
initCall := m.On("Init").Return(nil)
m.On("Do").Return(nil).NotBefore(initCall) Defensive patterns
Strategy: validation
Validate before calling
func callsFromMockOn(calls ...*mock.Call) bool {
for _, c := range calls {
if c == nil || reflect.ValueOf(c).Elem().FieldByName("Parent").IsNil() { return false }
}
return true
}
// before NotBefore: assert.True(t, callsFromMockOn(prereq)) Type guard
func callHasParent(c *mock.Call) bool {
// Parent is unexported; the only reliable guard is to ensure calls come from Mock.On()
return c != nil
} Try / catch
// Only pass calls returned by Mock.On() into NotBefore:
prereq := m.On("Init").Return(nil)
m.On("Do").Return(nil).NotBefore(prereq) Prevention
- Never construct *mock.Call literals by hand for ordering.
- Use mock.InOrder for sequential dependencies.
- Encapsulate call creation in a helper that always uses Mock.On().
When it happens
Trigger: Calling call.NotBefore(otherCall) where otherCall is a manually-constructed *mock.Call{...} or a zero-value Call. Also when passing a Call from a mock whose On() returned a partially-initialized value (e.g. a forked/patched testify).
Common situations: Building Call structs by hand to feed into NotBefore for sequencing; copying a Call between mocks incorrectly; using a Call after its Mock was reinitialized.
Related errors
- cannot use Func in expectations. Use mock.AnythingOfType("%T
- assert: arguments: %s is not a func
- assert: arguments: %s does not take exactly one argument
- assert: arguments: %s does not return a bool
- assert: arguments: Cannot call Get(%d) because there are %d
AI-assisted analysis of stretchr/testify@001eb7946b (2026-08-04).
Data as JSON: /data/errors/3869390355358de6.json.
Report an issue: GitHub.