{"record":{"id":"6c23ae7b6015eb18","repo":"devlooped/moq","slug":"to-specify-a-setup-for-public-property-0-1-use-the-typed","errorCode":null,"errorMessage":"To specify a setup for public property {0}.{1}, use the typed overloads, such as:\nmock.Setup(x => x.{1}).Returns(value);\nmock.SetupGet(x => x.{1}).Returns(value); //equivalent to previous one\nmock.SetupSet(x => x.{1}).Callback(callbackDelegate);\n","messagePattern":"To specify a setup for public property (.+?)\\.(.+?), use the typed overloads, such as:\nmock\\.Setup\\(x => x\\.(.+?)\\)\\.Returns\\(value\\);\nmock\\.SetupGet\\(x => x\\.(.+?)\\)\\.Returns\\(value\\); //equivalent to previous one\nmock\\.SetupSet\\(x => x\\.(.+?)\\)\\.Callback\\(callbackDelegate\\);\n","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/Protected/ProtectedMock.cs","lineNumber":427,"sourceCode":"        }\n\n        static void ThrowIfPublicMethod(MethodInfo method, string reflectedTypeName)\n        {\n            if (method.IsPublic)\n            {\n                throw new ArgumentException(string.Format(\n                    CultureInfo.CurrentCulture,\n                    Resources.MethodIsPublic,\n                    reflectedTypeName,\n                    method.Name));\n            }\n        }\n\n        static void ThrowIfPublicGetter(PropertyInfo property, string reflectedTypeName)\n        {\n            if (property.CanRead(out var getter) && getter.IsPublic)\n            {\n                throw new ArgumentException(string.Format(\n                    CultureInfo.CurrentCulture,\n                    Resources.UnexpectedPublicProperty,\n                    reflectedTypeName,\n                    property.Name));\n            }\n        }\n\n        static void ThrowIfPublicSetter(PropertyInfo property, string reflectedTypeName)\n        {\n            if (property.CanWrite(out var setter) && setter.IsPublic)\n            {\n                throw new ArgumentException(string.Format(\n                    CultureInfo.CurrentCulture,\n                    Resources.UnexpectedPublicProperty,\n                    reflectedTypeName,\n                    property.Name));\n            }\n        }","sourceCodeStart":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Protected/ProtectedMock.cs#L409-L445","documentation":"SetupGet/Setup-family APIs for protected members call ThrowIfPublicGetter to reject properties whose getter is public on the mocked type, throwing ArgumentException with Resources.UnexpectedPublicProperty that lists the typed-overload alternatives (Setup, SetupGet, SetupSet with lambdas). The Protected API must only be used for non-public property getters.","triggerScenarios":"mock.Protected().Setup<string>(\"PublicProp\") or SetupGet(\"PublicProp\") where T.PublicProp has a public getter — common after the property's accessibility changed to public.","commonSituations":"Property was protected in an older library version and became public; developer assumed non-public; Setup used for a getter-style property that is actually public.","solutions":["Use the strong-typed mock.Setup(x => x.Prop).Returns(v) or mock.SetupGet(x => x.Prop).Returns(v)","If only the getter is public but setter is protected, keep using Protected().SetupSet for the setter path","Update tests after accessibility changes in a library upgrade"],"exampleFix":"// before\nmock.Protected().Setup<string>(\"Name\");\n// after\nmock.SetupGet(x => x.Name).Returns(\"abc\"); // Name getter is public","handlingStrategy":"validation","validationCode":"static bool GetterIsProtected(Type t, string prop) =>\n    t.GetProperty(prop, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) is { } p\n    && p.GetGetMethod(true) is { } g && !g.IsPublic;","typeGuard":null,"tryCatchPattern":"try { mock.Protected().Setup<string>(\"Name\"); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"public property\")) { /* use mock.SetupGet(x => x.Name) */ }","preventionTips":["Verify getter accessibility before choosing Protected().Setup/SetupGet","Re-validate after library upgrades that change accessibility","Use SetupGet/SetupSet on Mock<T> for public getters","Document in test helpers which members are protected"],"tags":["moq","protected-members","properties","misuse"],"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"}