{"record":{"id":"fb544d79d6ee5e7b","repo":"devlooped/moq","slug":"resources-objectinstancenotmock","errorCode":null,"errorMessage":"Resources.ObjectInstanceNotMock","messagePattern":"Resources\\.ObjectInstanceNotMock","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/Mock.cs","lineNumber":108,"sourceCode":"                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();\n            }\n        }\n\n        /// <summary>\n        ///   Verifies all expectations regardless of whether they have been flagged as verifiable.\n        /// </summary>\n        /// <exception cref=\"MockException\">At least one expectation was not met.</exception>","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/Mock.cs#L90-L126","documentation":"Mock.Get<T>(mocked) only works on instances created by Moq, which expose the internal IMocked interface. This ArgumentException (parameter name 'mocked') is thrown when the object passed in is not a Moq-generated mock at all — e.g. a real implementation, a mock from another framework, or a compile-time T different from the runtime type.","triggerScenarios":"Passing a hand-written/stub instance (not produced by new Mock<T>().Object) to Mock.Get<T>; passing a fake from a different framework (FakeItEasy, NSubstitute); passing a real object returned by the mock rather than the mock's Object.","commonSituations":"Assuming a factory or DI container returned a Moq mock when it returned a concrete instance; mixing mock frameworks in test utilities; calling Mock.Get on the result of mock.Object.Method() instead of mock.Object.","solutions":["Ensure the object was created via new Mock<T>().Object (or Mock.Of<T>()) before calling Mock.Get","Pass mock.Object (the Moq proxy), not an arbitrary implementation of T","If you hold a real instance, keep a reference to the Mock<T> itself instead of deriving it from the object","Check whether another mocking framework produced the instance"],"exampleFix":"// before\nIFoo obj = new RealFoo();\nvar mock = Mock.Get<IFoo>(obj); // not a Moq mock\n// after\nvar mock = new Mock<IFoo>();\nIFoo obj = mock.Object; // same reference; Mock.Get works\nvar m = Mock.Get<IFoo>(obj);","handlingStrategy":"type-guard","validationCode":"static bool IsMoqMock(object instance)\n    => instance is IMocked;","typeGuard":"static bool TryGetMock<T>(object instance, out Mock mock)\n{\n    if (instance is IMocked m) { mock = m.Mock; return true; }\n    mock = null; return false;\n}","tryCatchPattern":"try\n{\n    var mock = Mock.Get<IFoo>(obj);\n}\ncatch (ArgumentException ex) when (ex.ParamName == \"mocked\")\n{\n    // 'obj' is not a Moq-created proxy; create/track a Mock<IFoo> instead\n    throw;\n}","preventionTips":["Only pass instances obtained from new Mock<T>().Object or Mock.Of<T>() into Mock.Get","Verify with a helper (instance is IMocked) before calling Mock.Get in shared test utilities","Avoid mixing mocking frameworks; keep one framework per test project","Pass mock.Object, not concrete implementations or results of calls on it"],"tags":["moq","mock","not-a-mock","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"}