{"record":{"id":"8d86f672d92d18c6","repo":"devlooped/moq","slug":"method-0-1-is-public-use-strong-typed-expect-overload","errorCode":null,"errorMessage":"Method {0}.{1} is public. Use strong-typed Expect overload instead:\nmock.Setup(x => x.{1}());\n","messagePattern":"Method (.+?)\\.(.+?) is public\\. Use strong-typed Expect overload instead:\nmock\\.Setup\\(x => x\\.(.+?)\\(\\)\\);\n","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/Protected/ProtectedMock.cs","lineNumber":415,"sourceCode":"                    }\n                }\n\n                throw new ArgumentException(string.Format(\n                    CultureInfo.CurrentCulture,\n                    Resources.MethodMissing,\n                    typeof(T).Name,\n                    methodName,\n                    string.Join(\n                        \", \",\n                        extractedTypeNames.ToArray())));\n            }\n        }\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        }","sourceCodeStart":397,"sourceCodeEnd":433,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Protected/ProtectedMock.cs#L397-L433","documentation":"ProtectedMock's InternalSetup/InternalSetupSequence/InternalVerify guard against accidentally using the Protected API for a public method. If the resolved member is public, ThrowIfPublicMethod throws ArgumentException with Resources.MethodIsPublic telling you to use the strong-typed Expect overload: mock.Setup(x => x.Method()). The Protected API is strictly for non-public members.","triggerScenarios":"mock.Protected().Setup(\"PublicMethod\") or .As<TAnalog>() setups where the matched method on T is public (e.g. the method was made public in a newer version, or the wrong member-name string resolved to a public method).","commonSituations":"Library upgrade changed a method from protected to public; developer assumed the method was protected without checking; analog-type rewriting resolved to a public member.","solutions":["Use the strong-typed API: mock.Setup(x => x.Method()) / mock.Verify(x => x.Method())","If you truly need protected semantics, confirm the method is still non-public in the version you mock","Pin/adjust for the library version — if the method became public, migrate the test to typed setups"],"exampleFix":"// before\nmock.Protected().Setup(\"Save\", ItExpr.IsAny<string>());\n// after\nmock.Setup(x => x.Save(It.IsAny<string>())); // Save is public","handlingStrategy":"validation","validationCode":"static bool IsProtected(Type t, string member) =>\n    t.GetMethod(member, BindingFlags.NonPublic | BindingFlags.Instance) is MethodInfo m && !m.IsPublic;","typeGuard":null,"tryCatchPattern":"try { mock.Protected().Setup(\"Save\", ItExpr.IsAny<string>()); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"is public\")) { /* switch to mock.Setup(x => x.Save(...)) */ }","preventionTips":["Check member accessibility with an IDE before using the Protected API","After upgrading a mocked library, re-check members that became public","Default to strong-typed Mock<T> setups for anything public","Keep a convention: Protected() only for members declared protected/protected internal"],"tags":["moq","protected-members","misuse","strong-typing"],"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"}