{"record":{"id":"80050c337dce25b7","repo":"devlooped/moq","slug":"out-expression-must-evaluate-to-a-constant-value","errorCode":null,"errorMessage":"Out expression must evaluate to a constant value.","messagePattern":"Out expression must evaluate to a constant value\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Moq/SetupWithOutParameterSupport.cs","lineNumber":49,"sourceCode":"                {\n                    invocation.Arguments[item.Key] = item.Value;\n                }\n            }\n        }\n\n        static List<KeyValuePair<int, object?>>? GetOutValues(IReadOnlyList<Expression> arguments, ParameterInfo[] parameters)\n        {\n            List<KeyValuePair<int, object?>>? outValues = null;\n            for (int i = 0, n = parameters.Length; i < n; ++i)\n            {\n                var parameter = parameters[i];\n                if (parameter.ParameterType.IsByRef)\n                {\n                    if ((parameter.Attributes & (ParameterAttributes.In | ParameterAttributes.Out)) == ParameterAttributes.Out)\n                    {\n                        if (arguments[i].PartialEval() is not ConstantExpression constant)\n                        {\n                            throw new NotSupportedException(Resources.OutExpressionMustBeConstantValue);\n                        }\n\n                        if (outValues == null)\n                        {\n                            outValues = new List<KeyValuePair<int, object?>>();\n                        }\n\n                        outValues.Add(new KeyValuePair<int, object?>(i, constant.Value));\n                    }\n                }\n            }\n            return outValues;\n        }\n    }\n}\n","sourceCodeStart":31,"sourceCodeEnd":65,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/SetupWithOutParameterSupport.cs#L31-L65","documentation":"When setting up methods with out parameters via this Moq helper, each out argument's expression must evaluate to a constant (PartialEval must yield a ConstantExpression) so Moq can produce a concrete value to assign to the out parameter. Anything non-constant (method calls, closures, computations) triggers this NotSupportedException.","triggerScenarios":"setup.TheOutParameter(() => ComputeDefault()) or any out-expression whose partial evaluation is not a constant, e.g. referencing instance state or invoking methods.","commonSituations":"Trying to compute out values dynamically in the expression; passing a property or method-call result as the out default instead of a literal or captured constant.","solutions":["Replace the expression with a constant or a pre-computed local variable: var v = ComputeDefault(); setup.TheOutParameter(v)","If a dynamic value is needed, use .Callback(...) or .Returns(...) to set the out parameter inside the delegate instead","Use mock.Protected/direct delegate-based setup where out values are assigned in a callback"],"exampleFix":"// before\nmock.Setup(m => m.TryParse(input, out DateTime.Now.AddMinutes(1)));\n// after\nvar ts = DateTime.Now.AddMinutes(1);\nmock.Setup(m => m.TryParse(input, out ts));","handlingStrategy":"validation","validationCode":"// ensure out expressions are constants: assign to a local first\nvar outVal = ComputeDefault(); // then use 'out outVal'","typeGuard":null,"tryCatchPattern":"try { mock.Setup(m => m.TryParse(input, out expr)); } catch (NotSupportedException) { /* pre-compute into a local and use 'out local' */ }","preventionTips":["Always pass constants or pre-computed locals as out expressions","Never call methods or access volatile state inside out expressions","Use Callback/Returns to assign out values when dynamics are required"],"tags":["moq","out-parameter","expression","not-supported"],"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"}