{"record":{"id":"3b07d304d33705af","repo":"devlooped/moq","slug":"resources-unsupportedexpression-formatted-with","errorCode":null,"errorMessage":"Resources.UnsupportedExpression (formatted with originalExpression)","messagePattern":"Resources\\.UnsupportedExpression \\(formatted with originalExpression\\)","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Moq/MatcherFactory.cs","lineNumber":196,"sourceCode":"                if (expression.IsMatch(out var match))\n                {\n                    return new Pair<IMatcher, Expression>(match, expression);\n                }\n            }\n\n            // Try reducing locals to get a constant.\n            var reduced = originalExpression.PartialEval();\n            if (reduced.NodeType == ExpressionType.Constant)\n            {\n                return new Pair<IMatcher, Expression>(new ConstantMatcher(((ConstantExpression)reduced).Value), reduced);\n            }\n\n            if (reduced.NodeType == ExpressionType.Quote)\n            {\n                return new Pair<IMatcher, Expression>(new ExpressionMatcher(((UnaryExpression)expression).Operand), reduced);\n            }\n\n            throw new NotSupportedException(\n                string.Format(CultureInfo.CurrentCulture, Resources.UnsupportedExpression, originalExpression));\n        }\n    }\n}\n","sourceCodeStart":178,"sourceCodeEnd":201,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/MatcherFactory.cs#L178-L201","documentation":"MatcherFactory throws this NotSupportedException when the argument expression in a setup cannot be reduced to a supported matcher form. Moq supports constant expressions, member access (property/field), matchers like It.*, quoted expression trees (ExpressionMatcher), and array initializers; anything else falls through to this error.","triggerScenarios":"Using an unsupported expression as a setup argument, e.g. method calls, arithmetic, lambdas, or other reducible-but-unsupported node types: `mock.Setup(m => m.Method(ComputeValue()))` or `m.Method(a + b)`.","commonSituations":"Developers embedding computed values or helper-method calls directly in setup expressions instead of matchers or constants.","solutions":["Pre-compute the value into a local variable and pass the constant","Replace the expression with an argument matcher: It.Is<T>(x => ...) or It.IsAny<T>()","For ref/expr arguments, pass a constant or quoted expression Moq can reduce"],"exampleFix":"// before\nmock.Setup(m => m.Calculate(x + y));\n// after\nvar sum = x + y;\nmock.Setup(m => m.Calculate(It.Is<int>(v => v == sum)));","handlingStrategy":"try-catch","validationCode":"// before setup, ensure arguments are constants or It matchers\nforeach (var arg in args) if (!(arg is ConstantExpression || arg?.NodeType == ExpressionType.MemberAccess)) throw new InvalidOperationException(\"Unsupported setup argument\");","typeGuard":"static bool IsSupportedSetupArg(Expression e) => e is ConstantExpression || e.NodeType is ExpressionType.MemberAccess or ExpressionType.Quote or ExpressionType.NewArrayInit;","tryCatchPattern":"try { mock.Setup(m => m.Method(Compute())); } catch (NotSupportedException ex) when (ex.Message.Contains(\"Unsupported expression\")) { /* hoist to local, use It.Is */ }","preventionTips":["Never call methods or do arithmetic inside setup expressions","Pre-compute values into locals before Setup","Use It.IsAny/It.Is for dynamic arguments"],"tags":["moq","expression-tree","unsupported-expression","setup"],"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"}