{"record":{"id":"d733cbb3e1b425c4","repo":"devlooped/moq","slug":"cannot-set-up-0-1-because-it-is-not-accessible-to-the-proxy","errorCode":null,"errorMessage":"Cannot set up {0}.{1} because it is not accessible to the proxy generator used by Moq:\n{2}","messagePattern":"Cannot set up (.+?)\\.(.+?) because it is not accessible to the proxy generator used by Moq:\n(.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/Guard.cs","lineNumber":116,"sourceCode":"            else if (!method.CanOverride())\n            {\n                throw new NotSupportedException(\n                    string.Format(\n                        CultureInfo.CurrentCulture,\n                        Resources.UnsupportedExpressionWithHint,\n                        expression.ToStringFixed(),\n                        string.Format(\n                            CultureInfo.CurrentCulture,\n                            Resources.UnsupportedNonOverridableMember,\n                            $\"{method.DeclaringType!.GetFormattedName()}.{method.Name}\")));\n            }\n        }\n\n        public static void IsVisibleToProxyFactory(MethodInfo method)\n        {\n            if (ProxyFactory.Instance.IsMethodVisible(method, out string? messageIfNotVisible) == false)\n            {\n                throw new ArgumentException(string.Format(\n                    CultureInfo.CurrentCulture,\n                    Resources.MethodNotVisibleToProxyFactory,\n                    method.DeclaringType!.Name,\n                    method.Name,\n                    messageIfNotVisible));\n            }\n        }\n\n        public static void IsEventAdd(LambdaExpression expression, string paramName)\n        {\n            Debug.Assert(expression != null);\n\n            switch (expression.Body.NodeType)\n            {\n                case ExpressionType.Call:\n                    var call = (MethodCallExpression)expression.Body;\n                    if (call.Method.IsEventAddAccessor()) return;\n                    break;","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Guard.cs#L98-L134","documentation":"Guard.IsVisibleToProxyFactory checks that the method being set up is visible to the dynamic proxy generator Moq uses (e.g. internal members not exposed via InternalsVisibleTo). If ProxyFactory reports the method as not visible, Moq throws ArgumentException with the type, method, and the proxy generator's explanation.","triggerScenarios":"Setting up or verifying internal or protected-internal members of an assembly that has not granted visibility to Moq's proxy (missing [assembly: InternalsVisibleTo(\"DynamicProxyGenAssembly2\")]) or to the test assembly.","commonSituations":"Mocking internal interfaces/classes across assembly boundaries; tests in a separate assembly mocking internal members; strong-named assemblies where InternalsVisibleTo needs the public key; switching from same-assembly to separate test-assembly layouts.","solutions":["Add [assembly: InternalsVisibleTo(\"DynamicProxyGenAssembly2\")] to the assembly containing the type (include PublicKey if strong-named).","Make the member public.","Mock a public interface instead of the internal type/member.","Move tests into the same assembly or adjust visibility of the member."],"exampleFix":"// before\n// AssemblyWithInternal: internal interface IThing { }\nmock.Setup(t => t.DoWork()); // throws\n// after\n// Add to AssemblyWithInternal Properties/AssemblyInfo.cs:\n[assembly: InternalsVisibleTo(\"DynamicProxyGenAssembly2\")]\nmock.Setup(t => t.DoWork());","handlingStrategy":"validation","validationCode":"var m = typeof(Thing).GetMethod(\"DoWork\", BindingFlags.NonPublic | BindingFlags.Instance)!;\nbool visible = m.IsPublic || m.IsFamilyOrAssembly || (m.IsAssembly && InternalsVisible.ToProxy());","typeGuard":null,"tryCatchPattern":"try { mock.Setup(t => t.DoWork()); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"not accessible to the proxy generator\")) { /* add InternalsVisibleTo(\"DynamicProxyGenAssembly2\") */ throw; }","preventionTips":["Add [assembly: InternalsVisibleTo(\"DynamicProxyGenAssembly2\")] (with public key for strong-named assemblies) when mocking internals.","Prefer mocking public interfaces.","Keep visibility modifiers intentional and documented per assembly."],"tags":["moq","internalsvisibleto","proxy"],"backgroundTag":"permission-denied","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"}