{"record":{"id":"c04bd6cc6c0f784f","repo":"devlooped/moq","slug":"resources-constructorargsfordelegate","errorCode":null,"errorMessage":"Resources.ConstructorArgsForDelegate","messagePattern":"Resources\\.ConstructorArgsForDelegate","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/Mock`1.cs","lineNumber":226,"sourceCode":"        {\n            var serialNumber = Interlocked.Increment(ref serialNumberCounter);\n\n            var name = new StringBuilder();\n            name.Append(\"Mock<\").AppendNameOf(typeof(T)).Append(':').Append(serialNumber).Append('>');\n            return name.ToString();\n        }\n\n        void CheckParameters()\n        {\n            if (this.constructorArguments.Length > 0)\n            {\n                if (typeof(T).IsInterface)\n                {\n                    throw new ArgumentException(Resources.ConstructorArgsForInterface);\n                }\n                if (typeof(T).IsDelegateType())\n                {\n                    throw new ArgumentException(Resources.ConstructorArgsForDelegate);\n                }\n            }\n        }\n\n        #endregion\n\n        #region Properties\n\n        /// <inheritdoc/>\n        public override MockBehavior Behavior => this.behavior;\n\n        /// <inheritdoc/>\n        public override bool CallBase\n        {\n            get => this.callBase;\n            set\n            {\n                if (value && this.MockedType.IsDelegateType())","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Mock`1.cs#L208-L244","documentation":"Moq throws this ArgumentException when constructor arguments are supplied to a Mock<T> whose T is a delegate type. Delegates have no class constructors to invoke, so ctor arguments cannot be applied to delegate mocks.","triggerScenarios":"new Mock<MyDelegateType>(someArg) or any Mock<T> ctor overload accepting constructor arguments when typeof(T).IsDelegateType(), raised in Mock<T>.CheckParameters (src/Moq/Mock`1.cs:226).","commonSituations":"Mocking Func/Action/custom delegate types (e.g. for HttpClient delegates or event handlers) while accidentally passing constructor arguments copied from a class-mock snippet.","solutions":["Create the delegate mock without arguments: new Mock<MyDelegateType>() and set up the invocation via Setup(d => d(It.IsAny<...>())).","If you actually need a concrete delegate instance, just use a lambda instead of mocking with ctor args.","Guard generic factory code so constructor args are only passed for non-delegate, non-interface classes."],"exampleFix":"// before\nvar mock = new Mock<Func<int, string>>(42);\n// after\nvar mock = new Mock<Func<int, string>>();","handlingStrategy":"validation","validationCode":"if (typeof(T).IsDelegateType() && ctorArgs is { Length: > 0 })\n    throw new ArgumentException(\"Constructor arguments are not valid for delegate mocks.\");\nvar mock = new Mock<T>();","typeGuard":"bool IsDelegateMock<T>() => typeof(T).IsSubclassOf(typeof(Delegate));","tryCatchPattern":"try { return new Mock<T>(args); } catch (ArgumentException) { return new Mock<T>(); }","preventionTips":["Create delegate mocks parameterless and configure invocations with Setup","Check IsDelegateType() in generic helpers before forwarding ctor args"],"tags":["moq","constructor-arguments","delegate","invalid-argument"],"backgroundTag":"invalid-constructor-argument","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"}