{"record":{"id":"1939f3da25dc4b32","repo":"devlooped/moq","slug":"property-0-1-does-not-have-a-setter","errorCode":null,"errorMessage":"Property {0}.{1} does not have a setter.","messagePattern":"Property (.+?)\\.(.+?) does not have a setter\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/Guard.cs","lineNumber":242,"sourceCode":"            }\n        }\n\n        public static void CanRead(PropertyInfo property)\n        {\n            if (!property.CanRead(out _))\n            {\n                throw new ArgumentException(string.Format(\n                    CultureInfo.CurrentCulture,\n                    Resources.PropertyGetNotFound,\n                    property.DeclaringType!.Name, property.Name));\n            }\n        }\n\n        public static void CanWrite(PropertyInfo property)\n        {\n            if (!property.CanWrite(out _))\n            {\n                throw new ArgumentException(string.Format(\n                    CultureInfo.CurrentCulture,\n                    Resources.PropertySetNotFound,\n                    property.DeclaringType!.Name, property.Name));\n            }\n        }\n    }\n}\n","sourceCodeStart":224,"sourceCodeEnd":250,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Guard.cs#L224-L250","documentation":"Moq throws this ArgumentException when setting up or reading a property that has no setter (read-only/getter-only property). Operations that require writing through the property, such as SetupProperty or SetupSet/VerifySet, fail because the property cannot be assigned.","triggerScenarios":"mock.SetupProperty(m => m.ReadOnlyProp); mock.SetupSet(m => m.ReadOnlyProp = ...); VerifySet on a getter-only property; DefaultValue.Mock with loose tracking on read-only property.","commonSituations":"Mocking immutable-style types exposing getter-only properties; trying to stub a computed/expression-bodied property; after a setter was removed during refactoring to immutability.","solutions":["Use mock.Setup(m => m.Prop).Returns(value) to stub the getter instead of writing the property.","For SetupProperty-style tracking, keep an auto-property with a setter or track via a callback.","If the property must be assignable in tests, add an internal/public setter (or constructor/setter injection) on the type.","Verify get behavior with VerifyGet rather than VerifySet for read-only properties."],"exampleFix":"// before\nmock.SetupProperty(m => m.Count); // Count is read-only\n// after\nmock.Setup(m => m.Count).Returns(5);","handlingStrategy":"type-guard","validationCode":"static bool HasSetter(Type t, string name) =>\n    t.GetProperty(name)?.GetSetMethod(nonPublic: false) != null;\n","typeGuard":"static bool IsWritable(System.Reflection.PropertyInfo p) => p.CanWrite;\n","tryCatchPattern":"try { mock.SetupProperty(expr); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"does not have a setter\"))\n{\n    // use Setup(...).Returns(...) instead of property-write setups\n}\n","preventionTips":["Check PropertyInfo.CanWrite before SetupProperty/SetupSet/VerifySet","Stub read-only properties with Setup(m => m.P).Returns(...)","Keep mutable state behind interfaces with settable properties when tests must write","Avoid SetupProperty on expression-bodied/getter-only properties"],"tags":["moq","property","setter","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"}