{"record":{"id":"604dd213c6fb570d","repo":"devlooped/moq","slug":"no-constructor-call-could-be-found","errorCode":null,"errorMessage":"No constructor call could be found.","messagePattern":"No constructor call could be found\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Moq/Expressions/Visitors/ConstructorCallVisitor.cs","lineNumber":34,"sourceCode":"    {\n        /// <summary>\n        /// Extracts the arguments from a lambda expression that calls a constructor.\n        /// </summary>\n        /// <param name=\"newExpression\">The constructor expression.</param>\n        /// <returns>Extracted argument values.</returns>\n        public static object[] ExtractArgumentValues(LambdaExpression newExpression)\n        {\n            if (newExpression is null)\n            {\n                throw new ArgumentNullException(nameof(newExpression));\n            }\n\n            var visitor = new ConstructorCallVisitor();\n            visitor.Visit(newExpression);\n\n            if (visitor.constructor == null)\n            {\n                throw new NotSupportedException(Resources.NoConstructorCallFound);\n            }\n\n            return visitor.arguments;\n        }\n\n        ConstructorInfo? constructor;\n        object[] arguments;\n\n#if NULLABLE_REFERENCE_TYPES\n        [return: NotNullIfNotNull(\"node\")]\n#endif\n        public override Expression? Visit(Expression? node)\n        {\n            switch (node?.NodeType)\n            {\n                case null:\n                    return null;\n                case ExpressionType.Lambda:","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Expressions/Visitors/ConstructorCallVisitor.cs#L16-L52","documentation":"ExtractArgumentValues visits the lambda expecting its body to contain a NewExpression (constructor call). If the visitor finds no constructor call (visitor.constructor == null), it throws NotSupportedException 'No constructor call could be found.' The lambda must literally be something like `x => new Foo(x.A, x.B)`.","triggerScenarios":"Passing a lambda whose body is not `new ...`: member accesses, method calls, `x => x.Service` factory-style expressions, object initializers compiled unexpectedly, or lambdas returning results of static factory methods instead of constructor invocation.","commonSituations":"Fixture/AutoFixture-style setups where a factory method is used instead of a constructor; lambdas written as `x => x.CreateFoo()`; expressions with converted/quoted bodies (Convert nodes) hiding the NewExpression; generic helpers passing `default` placeholder lambdas.","solutions":["Change the lambda to directly instantiate the type: `x => new MyService(x.A, x.B)`.","If construction goes through a factory method, refactor so the visitor receives the constructor call, or handle factory logic outside this API.","Check for Convert/cast nodes wrapping the body and use an untyped lambda so the body is a plain NewExpression.","Inspect the lambda string in your setup: it must start with `new `; if not, this visitor is the wrong tool for that expression."],"exampleFix":"// before\nvar args = ConstructorCallVisitor.ExtractArgumentValues((Expression<Func<Dep, IService>>)(d => d.GetService()));\n// after\nvar args = ConstructorCallVisitor.ExtractArgumentValues((Expression<Func<Dep, IService>>)(d => new Service(d.Logger)));","handlingStrategy":"type-guard","validationCode":"static bool HasConstructorCall(LambdaExpression e) =>\n    Unwrap(e.Body) is NewExpression;","typeGuard":"static Expression Unwrap(Expression e) =>\n    e is UnaryExpression { NodeType: ExpressionType.Convert or ExpressionType.Quote } u ? Unwrap(u.Operand) : e;","tryCatchPattern":"try\n{\n    var values = ConstructorCallVisitor.ExtractArgumentValues(lambda);\n}\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"No constructor call\"))\n{\n    // rewrite as x => new T(...) or use a different extraction path\n}","preventionTips":["Lambdas for ExtractArgumentValues must literally contain `new T(...)`","Avoid factory methods in expressions destined for constructor extraction","Beware Convert wrappers: use untyped lambdas so the body stays a NewExpression","Validate the body node type before invoking the visitor in helper code"],"tags":["moq","not-supported","expression-visitor","constructor"],"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"}