{"record":{"id":"16b8f1b7dec8049a","repo":"devlooped/moq","slug":"resources-callbasecannotbeusedwithdelegatemocks","errorCode":null,"errorMessage":"Resources.CallBaseCannotBeUsedWithDelegateMocks","messagePattern":"Resources\\.CallBaseCannotBeUsedWithDelegateMocks","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Moq/MethodCall.cs","lineNumber":134,"sourceCode":"                }\n                else\n                {\n                    new ReturnBaseOrDefaultValue(this.Mock).Execute(invocation);\n                }\n            }\n            else\n            {\n                HandleEventSubscription.Handle(invocation, this.Mock);  // no-op for everything other than event accessors\n            }\n\n            this.afterReturnCallback?.Execute(invocation);\n        }\n\n        public void SetCallBaseBehavior()\n        {\n            if (this.Mock.MockedType.IsDelegateType())\n            {\n                throw new NotSupportedException(Resources.CallBaseCannotBeUsedWithDelegateMocks);\n            }\n\n            this.returnOrThrow = ReturnBase.Instance;\n        }\n\n        public void SetCallbackBehavior(Delegate callback)\n        {\n            if (callback == null)\n            {\n                throw new ArgumentNullException(nameof(callback));\n            }\n\n            ref Behavior? behavior = ref (this.returnOrThrow == null) ? ref this.callback\n                                                                     : ref this.afterReturnCallback;\n\n            if (callback is Action callbackWithoutArguments)\n            {\n                behavior = new Callback(_ => callbackWithoutArguments());","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/MethodCall.cs#L116-L152","documentation":"MethodCall.SetCallBaseBehavior throws NotSupportedException because CallBase cannot be used when the mocked type is a delegate. Delegates have no base implementation to invoke, so the CallBase behavior is meaningless and rejected.","triggerScenarios":"Setting up a delegate mock (e.g. Mock<Func<int,int>> or Mock<EventHandler>) and calling `.CallBase()` on the setup or mock.","commonSituations":"Developers mocking delegates (common in event handler or legacy callback scenarios) who attempt CallBase to fall through to the delegate's original target.","solutions":["Remove the CallBase() call for delegate mocks","Provide an explicit Returns()/callback implementation instead","If base behavior is needed, mock an interface or class with a real implementation rather than a delegate"],"exampleFix":"// before\nvar mock = new Mock<Func<int, int>>();\nmock.CallBase = true;\n// after\nvar mock = new Mock<Func<int, int>>();\nmock.Setup(f => f(42)).Returns(100);","handlingStrategy":"validation","validationCode":"if (mockedType.IsDelegateType()) throw new InvalidOperationException(\"CallBase is not supported for delegate mocks\");","typeGuard":"static bool IsDelegateMock<T>() => typeof(T).IsSubclassOf(typeof(Delegate)) || typeof(T) == typeof(Delegate);","tryCatchPattern":"try { mock.CallBase = true; } catch (NotSupportedException ex) when (ex.Message.Contains(\"delegate\")) { /* use explicit Returns instead */ }","preventionTips":["Never set CallBase on Mock<T> where T is a delegate","Use Returns/callbacks for delegate mocks","Guard helper code that generically enables CallBase"],"tags":["moq","callbase","delegate-mock","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"89a5be629c752960fe403e95ff583e4ae6f00542","analyzedAt":"2026-09-16T05:31:39.496Z","contentChangedAt":"2026-09-16T05:31:39.496Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}