{"record":{"id":"f44caed5cbd1ac05","repo":"devlooped/moq","slug":"unhandled-expression-type-0","errorCode":null,"errorMessage":"Unhandled expression type: {0}","messagePattern":"Unhandled expression type: (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/Moq/StringBuilderExtensions.AppendExpression.cs","lineNumber":122,"sourceCode":"\n                case ExpressionType.Invoke:\n                    return builder.AppendExpression((InvocationExpression)expression);\n\n                case ExpressionType.MemberInit:\n                    return builder.AppendExpression((MemberInitExpression)expression);\n\n                case ExpressionType.ListInit:\n                    return builder.AppendExpression((ListInitExpression)expression);\n\n                case ExpressionType.Extension:\n                    if (expression is MatchExpression me)\n                    {\n                        return builder.AppendExpression(me);\n                    }\n                    goto default;\n\n                default:\n                    throw new Exception(string.Format(Resources.UnhandledExpressionType, expression.NodeType));\n            }\n        }\n\n        static StringBuilder AppendElementInit(this StringBuilder builder, ElementInit initializer)\n        {\n            return builder.AppendCommaSeparated(\"{ \", initializer.Arguments, AppendExpression, \" }\");\n        }\n\n        static StringBuilder AppendExpression(this StringBuilder builder, UnaryExpression expression)\n        {\n            switch (expression.NodeType)\n            {\n                case ExpressionType.Convert:\n                case ExpressionType.ConvertChecked:\n                    return builder.Append('(')\n                                  .AppendNameOf(expression.Type)\n                                  .Append(')')\n                                  .AppendExpression(expression.Operand);","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/StringBuilderExtensions.AppendExpression.cs#L104-L140","documentation":"Moq's expression-to-string formatter (used to render lambda expressions like `x => x.Foo()` in verification messages) walks the expression tree with a switch over node types. When it encounters an expression node type it has no case for, it throws this generic Exception with the unhandled NodeType in the message. It is an internal completeness guard, meaning Moq's AppendExpression does not support that expression construct.","triggerScenarios":"Passing a lambda to Mock.Of/Verify/Setup whose body uses an expression node type Moq cannot stringify — e.g. switch expressions, coalesce assignment, dynamic operations, index initializers in unusual positions, or newer C# expression forms added after the formatter was written.","commonSituations":"Using newer C# language features (C# 8/9+ syntax) inside setup/verify expressions; complex nested member-init or list-init expressions; upgrading the compiler/framework so previously unsupported nodes appear.","solutions":["Identify the NodeType printed in the message and simplify the expression to constructs Moq supports (member access, method calls, simple binary/conditional expressions)","Extract the unsupported construct into a plain helper method called from the expression where possible, or compute values outside the lambda and use a variable","Verify with a different overload, e.g. capture the expression differently or use It.Is<T> predicates inside supported calls","If the node type is a common one, file/patch an issue in Moq's StringBuilderExtensions.AppendExpression switch"],"exampleFix":"// before\nmock.Verify(m => m.Foo(x => x switch { 1 => \"a\", _ => \"b\" }));\n// after\nstring Pick(int x) => x switch { 1 => \"a\", _ => \"b\" };\nmock.Verify(m => m.Foo(x => Pick(x)));","handlingStrategy":"validation","validationCode":"// keep setup/verify lambdas to member access, method calls and simple operators\nExpression<Func<T, TResult>> expr = x => x.Foo(1); // supported\n// avoid: switch expressions, ??=, dynamic, pattern constructs inside the lambda","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Stick to simple expression bodies in Moq setup/verify lambdas","Compute complex logic outside the expression and pass variables","When upgrading C#, re-run tests covering Verify message rendering","Watch the NodeType in the message to identify the unsupported construct"],"tags":["expression-tree","moq","unsupported-syntax"],"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"}