{"id":"3869390355358de6","repo":"stretchr/testify","slug":"not-before-calls-must-be-created-with-mock-on","errorCode":null,"errorMessage":"not before calls must be created with Mock.On()","messagePattern":"not before calls must be created with Mock\\.On\\(\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mock/mock.go","lineNumber":275,"sourceCode":"\t}\n\n\treturn c\n}\n\n// NotBefore indicates that the mock should only be called after the referenced\n// calls have been called as expected. The referenced calls may be from the\n// same mock instance and/or other mock instances.\n//\n//\tMock.On(\"Do\").Return(nil).NotBefore(\n//\t    Mock.On(\"Init\").Return(nil)\n//\t)\nfunc (c *Call) NotBefore(calls ...*Call) *Call {\n\tc.lock()\n\tdefer c.unlock()\n\n\tfor _, call := range calls {\n\t\tif call.Parent == nil {\n\t\t\tpanic(\"not before calls must be created with Mock.On()\")\n\t\t}\n\t}\n\n\tc.requires = append(c.requires, calls...)\n\treturn c\n}\n\n// InOrder defines the order in which the calls should be made\n//\n//\tFor example:\n//\n//\tInOrder(\n//\t\tMock.On(\"init\").Return(nil),\n//\t\tMock.On(\"Do\").Return(nil),\n//\t)\nfunc InOrder(calls ...*Call) {\n\tfor i := 1; i < len(calls); i++ {\n\t\tcalls[i].NotBefore(calls[i-1])","sourceCodeStart":257,"sourceCodeEnd":293,"githubUrl":"https://github.com/stretchr/testify/blob/001eb7946baf451879253643e4ce4b38eaa0d4a7/mock/mock.go#L257-L293","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nc := &mock.Call{Method: \"Init\"}\nm.On(\"Do\").Return(nil).NotBefore(c) // panic\n// after\ninitCall := m.On(\"Init\").Return(nil)\nm.On(\"Do\").Return(nil).NotBefore(initCall)","handlingStrategy":"validation","validationCode":"func callsFromMockOn(calls ...*mock.Call) bool {\n    for _, c := range calls {\n        if c == nil || reflect.ValueOf(c).Elem().FieldByName(\"Parent\").IsNil() { return false }\n    }\n    return true\n}\n// before NotBefore: assert.True(t, callsFromMockOn(prereq))","typeGuard":"func callHasParent(c *mock.Call) bool {\n    // Parent is unexported; the only reliable guard is to ensure calls come from Mock.On()\n    return c != nil\n}","tryCatchPattern":"// Only pass calls returned by Mock.On() into NotBefore:\nprereq := m.On(\"Init\").Return(nil)\nm.On(\"Do\").Return(nil).NotBefore(prereq)","preventionTips":["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()."],"tags":["mock","panic","notbefore","ordering","call-construction"],"analyzedSha":"001eb7946baf451879253643e4ce4b38eaa0d4a7","analyzedAt":"2026-08-04T21:49:58.715Z","schemaVersion":2}