devlooped/moq · error · ArgumentException
Resources.ConstructorArgsForDelegate
Error message
Resources.ConstructorArgsForDelegate
What it means
Moq throws this ArgumentException when constructor arguments are supplied to a Mock<T> whose T is a delegate type. Delegates have no class constructors to invoke, so ctor arguments cannot be applied to delegate mocks.
Solutions
- Create the delegate mock without arguments: new Mock<MyDelegateType>() and set up the invocation via Setup(d => d(It.IsAny<...>())).
- If you actually need a concrete delegate instance, just use a lambda instead of mocking with ctor args.
- Guard generic factory code so constructor args are only passed for non-delegate, non-interface classes.
Example fix
// before var mock = new Mock<Func<int, string>>(42); // after var mock = new Mock<Func<int, string>>();
Defensive patterns
Strategy: validation
Validate before calling
if (typeof(T).IsDelegateType() && ctorArgs is { Length: > 0 })
throw new ArgumentException("Constructor arguments are not valid for delegate mocks.");
var mock = new Mock<T>(); Type guard
bool IsDelegateMock<T>() => typeof(T).IsSubclassOf(typeof(Delegate));
Try / catch
try { return new Mock<T>(args); } catch (ArgumentException) { return new Mock<T>(); } Prevention
- Create delegate mocks parameterless and configure invocations with Setup
- Check IsDelegateType() in generic helpers before forwarding ctor args
When it happens
Trigger: new Mock<MyDelegateType>(someArg) or any Mock<T> ctor overload accepting constructor arguments when typeof(T).IsDelegateType(), raised in Mock<T>.CheckParameters (src/Moq/Mock`1.cs:226).
Common situations: Mocking Func/Action/custom delegate types (e.g. for HttpClient delegates or event handlers) while accidentally passing constructor arguments copied from a class-mock snippet.
Related errors
- Resources.ConstructorArgsForInterface
- Resources.CallBaseCannotBeUsedWithDelegateMocks
- Resources.AsMustBeInterface
- Unsupported expression
- Could not determine the correct positions for all argument…
AI-assisted analysis of devlooped/moq@89a5be629c (2026-09-16).
Data as JSON: /api/errors/c04bd6cc6c0f784f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Moq/Mock`1.cs:226
{
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
{
get => this.callBase;
set
{
if (value && this.MockedType.IsDelegateType())View on GitHub (pinned to 89a5be629c)