{"record":{"id":"f580b7f273dd6827","repo":"devlooped/moq","slug":"a-matching-constructor-for-the-given-arguments-was-not-found","errorCode":null,"errorMessage":"A matching constructor for the given arguments was not found on the mocked type.","messagePattern":"A matching constructor for the given arguments was not found on the mocked type\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/Interception/CastleProxyFactory.cs","lineNumber":78,"sourceCode":"            else if (mockType.IsDelegateType())\n            {\n                var options = new ProxyGenerationOptions();\n                options.AddDelegateTypeMixin(mockType);\n                var container = GetClassGenerator(mockType).CreateClassProxy(typeof(object), additionalInterfaces, options, new Interceptor(interceptor));\n                return Delegate.CreateDelegate(mockType, container, container.GetType().GetMethod(\"Invoke\")!);\n            }\n\n            try\n            {\n                return GetClassGenerator(mockType).CreateClassProxy(mockType, additionalInterfaces, this.generationOptions, arguments, new Interceptor(interceptor));\n            }\n            catch (TypeLoadException e)\n            {\n                throw new ArgumentException(string.Format(Resources.TypeNotMockable, mockType), e);\n            }\n            catch (MissingMethodException e)\n            {\n                throw new ArgumentException(Resources.ConstructorNotFound, e);\n            }\n        }\n\n        public override bool IsMethodVisible(MethodInfo method, out string messageIfNotVisible)\n        {\n            return ProxyUtil.IsAccessible(method, out messageIfNotVisible);\n        }\n\n        public override bool IsTypeVisible(Type type)\n        {\n            return ProxyUtil.IsAccessible(type);\n        }\n\n        sealed class Interceptor : Castle.DynamicProxy.IInterceptor\n        {\n            static readonly MethodInfo proxyInterceptorGetter = typeof(IProxy).GetProperty(nameof(IProxy.Interceptor))!.GetMethod!;\n\n            Moq.IInterceptor interceptor;","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Interception/CastleProxyFactory.cs#L60-L96","documentation":"In CastleProxyFactory.CreateProxy, Castle DynamicProxy signals that no constructor matching the supplied arguments exists on the mocked type by throwing MissingMethodException; Moq converts this into an ArgumentException with the ConstructorNotFound message. Moq passes constructor arguments only when you call Mock<T> with a ctorArgs array, and those arguments must bind to a real constructor of the (non-interface) mocked type.","triggerScenarios":"new Mock<MyClass>(ctorArgs) where no constructor of MyClass accepts the given number/types of arguments, or passing constructor arguments when mocking an interface/delegate that has no constructor at all.","commonSituations":"Refactoring a class constructor (changing parameter types or counts) without updating the mock setup in tests; passing nulls or mismatched types so overload resolution fails; copying a mock setup between a class and its interface.","solutions":["Check the mocked type's constructors and pass arguments that exactly match one (types and count).","If the class has a parameterless constructor, call new Mock<T>() with no args.","Pass typed values (or null with explicit cast) instead of loosely typed objects so the right overload binds.","If mocking an interface, remove the constructor arguments entirely."],"exampleFix":"// before\nvar mock = new Mock<MyService>(\"conn\", 42); // ctor takes (string, string)\n// after\nvar mock = new Mock<MyService>(\"conn\", \"default\");","handlingStrategy":"validation","validationCode":"bool ctorMatches = typeof(T).GetConstructors().Any(c =>\n    c.GetParameters().Length == args.Length &&\n    c.GetParameters().Zip(args, (p, a) => a == null || p.ParameterType.IsInstanceOfType(a)).All(ok => ok));\nif (!ctorMatches) throw new InvalidOperationException(\"No matching constructor for mock args.\");","typeGuard":null,"tryCatchPattern":"try { var mock = new Mock<T>(args); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"matching constructor\")) { /* fix args or use parameterless ctor */ }","preventionTips":["Prefer the parameterless constructor or set properties via Mock<T>.Object property injection instead of ctor args.","Keep constructor signatures stable; update all new Mock<T>(...) call sites when refactoring ctors.","Match argument types exactly — use explicit casts for nulls: new Mock<T>((string)null)."],"tags":["moq","mocking","constructor","missing-method","csharp"],"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"}