devlooped/moq · error · ArgumentException

Resources.ObjectInstanceNotMock

Error message

Resources.ObjectInstanceNotMock

What it means

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.

Solutions

  1. Ensure the object was created via new Mock<T>().Object (or Mock.Of<T>()) before calling Mock.Get
  2. Pass mock.Object (the Moq proxy), not an arbitrary implementation of T
  3. If you hold a real instance, keep a reference to the Mock<T> itself instead of deriving it from the object
  4. Check whether another mocking framework produced the instance

Example fix

// before
IFoo obj = new RealFoo();
var mock = Mock.Get<IFoo>(obj); // not a Moq mock
// after
var mock = new Mock<IFoo>();
IFoo obj = mock.Object; // same reference; Mock.Get works
var m = Mock.Get<IFoo>(obj);
Defensive patterns

Strategy: type-guard

Validate before calling

static bool IsMoqMock(object instance)
    => instance is IMocked;

Type guard

static bool TryGetMock<T>(object instance, out Mock mock)
{
    if (instance is IMocked m) { mock = m.Mock; return true; }
    mock = null; return false;
}

Try / catch

try
{
    var mock = Mock.Get<IFoo>(obj);
}
catch (ArgumentException ex) when (ex.ParamName == "mocked")
{
    // 'obj' is not a Moq-created proxy; create/track a Mock<IFoo> instead
    throw;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of devlooped/moq@89a5be629c (2026-09-16). Data as JSON: /api/errors/fb544d79d6ee5e7b. Report an issue: GitHub.

Appendix: source

Thrown at src/Moq/Mock.cs:108

                var imockedType = mocked.GetType().GetInterfaces().Single(i => i.Name.Equals("IMocked`1", StringComparison.Ordinal));
                var mockedType = imockedType.GetGenericArguments()[0];
                var types = string.Join(
                    ", ",
                    new[] { mockedType }
                        // Ignore internally defined IMocked<T>
                        .Concat(mock.InheritedInterfaces)
                        .Concat(mock.AdditionalInterfaces)
                        .Select(t => t.Name)
                        .ToArray());

                throw new ArgumentException(string.Format(
                    CultureInfo.CurrentCulture,
                    Resources.InvalidMockGetType,
                    typeof(T).Name,
                    types));
            }

            throw new ArgumentException(Resources.ObjectInstanceNotMock, "mocked");
        }

        /// <summary>
        ///   Verifies that all verifiable expectations have been met.
        /// </summary>
        /// <exception cref="MockException">Not all verifiable expectations were met.</exception>
        public static void Verify(params Mock[] mocks)
        {
            foreach (var mock in mocks)
            {
                mock.Verify();
            }
        }

        /// <summary>
        ///   Verifies all expectations regardless of whether they have been flagged as verifiable.
        /// </summary>
        /// <exception cref="MockException">At least one expectation was not met.</exception>

View on GitHub (pinned to 89a5be629c)