{"record":{"id":"ad88b7ae6c02a372","repo":"devlooped/moq","slug":"resources-invalidmockgettype-formatted-with-typeof-t-name","errorCode":null,"errorMessage":"Resources.InvalidMockGetType (formatted with typeof(T).Name and mocked type list)","messagePattern":"Resources\\.InvalidMockGetType \\(formatted with typeof\\(T\\)\\.Name and mocked type list\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/Mock.cs","lineNumber":101,"sourceCode":"\n                // Alternatively, we may have been asked \n                // for a type that is assignable to the \n                // one for the mock.\n                // This is not valid as generic types \n                // do not support covariance on \n                // the generic parameters.\n                var imockedType = mocked.GetType().GetInterfaces().Single(i => i.Name.Equals(\"IMocked`1\", StringComparison.Ordinal));\n                var mockedType = imockedType.GetGenericArguments()[0];\n                var types = string.Join(\n                    \", \",\n                    new[] { mockedType }\n                        // Ignore internally defined IMocked<T>\n                        .Concat(mock.InheritedInterfaces)\n                        .Concat(mock.AdditionalInterfaces)\n                        .Select(t => t.Name)\n                        .ToArray());\n\n                throw new ArgumentException(string.Format(\n                    CultureInfo.CurrentCulture,\n                    Resources.InvalidMockGetType,\n                    typeof(T).Name,\n                    types));\n            }\n\n            throw new ArgumentException(Resources.ObjectInstanceNotMock, \"mocked\");\n        }\n\n        /// <summary>\n        ///   Verifies that all verifiable expectations have been met.\n        /// </summary>\n        /// <exception cref=\"MockException\">Not all verifiable expectations were met.</exception>\n        public static void Verify(params Mock[] mocks)\n        {\n            foreach (var mock in mocks)\n            {\n                mock.Verify();","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Mock.cs#L83-L119","documentation":"Mock.Get<T>(mocked) retrieves the Mock<T> object behind a mock instance. This ArgumentException is thrown when the requested T is not part of the mock's type graph — the instance is a mock, but not for (or assignable through) the requested type, since .NET generics do not support covariance here. The message lists T and the types the mock actually implements.","triggerScenarios":"Mock.Get<SomeOtherInterface>(mockOfIFoo) where the mock was not created for or As<T>'d to that interface; calling Mock.Get on the class-mocked type when only interfaces were mocked; requesting a derived type from a base-type mock.","commonSituations":"Passing a mock instance to helper code typed with a different interface; after refactoring, requesting the concrete type when the mock was created from an interface; forgetting a required mock.As<T>() registration.","solutions":["Request the same type the mock was created with (Mock.Get<IFoo>(mockOfIFoo))","If you need another interface, register it first with mock.As<IOther>() and then call Mock.Get<IOther>","Create a separate Mock<T> for the additional type instead of reusing this instance","Check the types listed in the message to see which interfaces the mock actually implements"],"exampleFix":"// before\nvar mock = new Mock<IFoo>();\nvar m = Mock.Get<IBar>(mock.Object); // IBar not implemented by this mock\n// after\nmock.As<IBar>();\nvar m = Mock.Get<IBar>(mock.Object);","handlingStrategy":"try-catch","validationCode":"static bool CanGetMock<T>(object mocked)\n{\n    return mocked is IMocked m && m.Mock.ImplementsInterface(typeof(T));\n}","typeGuard":"static bool IsMockOfType<T>(object instance)\n    => instance is IMocked m && m.Mock.ImplementsInterface(typeof(T));","tryCatchPattern":"try\n{\n    var mock = Mock.Get<IFoo>(obj);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(typeof(IFoo).Name))\n{\n    // the mock was not created for/As<T>'d to IFoo — register or create it\n    throw;\n}","preventionTips":["Always call Mock.Get with the exact type the mock was created with","Use mock.As<TOther>() before retrieving Mock.Get<TOther>","Track Mock<T> instances directly instead of deriving them from Object"],"tags":["moq","mock","generic-type","unit-testing"],"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"}