devlooped/moq · error · ArgumentException

Resources.ConstructorArgsForInterface

Error message

Resources.ConstructorArgsForInterface

What it means

Moq throws this ArgumentException when a Mock<T> is constructed with constructor arguments but T is an interface. Interfaces have no constructors, so constructor arguments are meaningless for interface mocks; Moq generates the implementation itself and never calls a ctor.

Solutions

  1. Remove the constructor arguments — create the mock with new Mock<IInterface>() and configure behavior via Setup instead.
  2. If you need constructor injection, mock the concrete class instead: new Mock<ConcreteClass>(args) { CallBase = true }.
  3. In generic helper code, branch on typeof(T).IsInterface and only pass args when T is a class.

Example fix

// before
var mock = new Mock<IRepository>(connectionString);
// after
var mock = new Mock<IRepository>();
Defensive patterns

Strategy: validation

Validate before calling

if (typeof(T).IsInterface && ctorArgs is { Length: > 0 })
    throw new ArgumentException("Constructor arguments are not valid for interface mocks.");
var mock = new Mock<T>();

Type guard

bool SupportsCtorArgs<T>() => !typeof(T).IsInterface && !typeof(T).IsSubclassOf(typeof(Delegate));

Try / catch

try { var mock = new Mock<T>(args); } catch (ArgumentException ex) when (ex.Message.Contains("constructor")) { /* fall back to new Mock<T>() */ }

Prevention

When it happens

Trigger: new Mock<ISomeInterface>(ctorArg1, ctorArg2) or Mock.Of with constructor arg — any Mock<T> ctor overload taking args/values when typeof(T).IsInterface, raised in Mock<T>.CheckParameters (src/Moq/Mock`1.cs:222).

Common situations: Copy-pasting a class mock setup and only changing the type to an interface; generic helper code that passes ctor args for all mocked types; migrating from a class-based mock to an interface without removing the args.

Related errors


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

Appendix: source

Thrown at src/Moq/Mock`1.cs:222

        {
        }

        static string CreateUniqueDefaultMockName()
        {
            var serialNumber = Interlocked.Increment(ref serialNumberCounter);

            var name = new StringBuilder();
            name.Append("Mock<").AppendNameOf(typeof(T)).Append(':').Append(serialNumber).Append('>');
            return name.ToString();
        }

        void CheckParameters()
        {
            if (this.constructorArguments.Length > 0)
            {
                if (typeof(T).IsInterface)
                {
                    throw new ArgumentException(Resources.ConstructorArgsForInterface);
                }
                if (typeof(T).IsDelegateType())
                {
                    throw new ArgumentException(Resources.ConstructorArgsForDelegate);
                }
            }
        }

        #endregion

        #region Properties

        /// <inheritdoc/>
        public override MockBehavior Behavior => this.behavior;

        /// <inheritdoc/>
        public override bool CallBase
        {

View on GitHub (pinned to 89a5be629c)