{"record":{"id":"75eae045cd0284ec","repo":"devlooped/moq","slug":"expression-is-not-a-setter-0","errorCode":null,"errorMessage":"Expression is not a setter: {0}","messagePattern":"Expression is not a setter: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/Guard.cs","lineNumber":76,"sourceCode":"\n        public static void IsAssignmentToPropertyOrIndexer(LambdaExpression expression, string paramName)\n        {\n            Debug.Assert(expression != null);\n\n            switch (expression.Body.NodeType)\n            {\n                case ExpressionType.Assign:\n                    var assignment = (BinaryExpression)expression.Body;\n                    if (assignment.Left is MemberExpression || assignment.Left is IndexExpression) return;\n                    break;\n\n                case ExpressionType.Call:\n                    var call = (MethodCallExpression)expression.Body;\n                    if (call.Method.IsSetAccessor()) return;\n                    break;\n            }\n\n            throw new ArgumentException(\n                string.Format(\n                    CultureInfo.CurrentCulture,\n                    Resources.SetupNotSetter,\n                    expression.ToStringFixed()),\n                paramName);\n        }\n\n        public static void IsOverridable(MethodInfo method, Expression expression)\n        {\n            if (method.IsStatic)\n            {\n                throw new NotSupportedException(\n                    string.Format(\n                        CultureInfo.CurrentCulture,\n                        Resources.UnsupportedExpressionWithHint,\n                        expression.ToStringFixed(),\n                        string.Format(\n                            CultureInfo.CurrentCulture,","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Guard.cs#L58-L94","documentation":"This guard validates that a lambda expression passed to a setup API is an assignment to a property or indexer (a setter), e.g. mock.SetupSet(x => x.Prop = value). If the expression body is not an assignment whose target is a property/indexer (or a Call that is a set accessor), Moq throws ArgumentException. It ensures SetupSet/SetupOnlySet receive valid setter expressions.","triggerScenarios":"Calling mock.SetupSet with a getter expression (x => x.Prop), a method call that is not a set accessor, a field assignment, or a compound/unsupported body.","commonSituations":"Confusing Setup with SetupSet (passing a getter where a setter is required); assigning to a public field instead of a property; setting up read-only or expression-bodied properties.","solutions":["Use an assignment expression in SetupSet: mock.SetupSet(x => x.Prop = It.IsAny<string>()).","Use mock.Setup(x => x.Prop) instead if you meant a getter setup.","If assigning a field, convert the field to a property (or mock the interface) since fields cannot be mocked.","Verify you are calling SetupSet/SetupOnlySet, not Setup."],"exampleFix":"// before\nmock.SetupSet(x => x.Name); // getter, not a setter\n// after\nmock.SetupSet(x => x.Name = It.IsAny<string>());","handlingStrategy":"validation","validationCode":"bool isSetterLambda(Expression<Func<T,object>> e) => e.Body is BinaryExpression b && b.NodeType == ExpressionType.Assign && (b.Left is MemberExpression || b.Left is IndexExpression);","typeGuard":"static bool IsSetterExpr<T>(Expression<Func<T, object>> e) => e.Body is BinaryExpression { NodeType: ExpressionType.Assign, Left: MemberExpression or IndexExpression };","tryCatchPattern":"try { mock.SetupSet(x => x.Name = It.IsAny<string>()); }\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Expression is not a setter\")) { /* use Setup for getters instead */ throw; }","preventionTips":["Use SetupSet only with assignment lambdas (x => x.Prop = value).","Use Setup for getter/read setups.","Convert public fields to properties before mocking.","Do not wrap the lambda body in casts that hide the assignment."],"tags":["moq","expression-tree","setter"],"backgroundTag":"invalid-argument-value","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"}