devlooped/moq · error · ArgumentNullException
value (Argument is null)
Error message
value (Argument is null)
What it means
MockFactory.DefaultValueProvider is a non-nullable property, so its setter rejects null with ArgumentNullException(paramName: 'value'). Assigning null would leave the factory without a default value provider, which Moq treats as a programming error.
Solutions
- Only assign a real DefaultValueProvider instance (DefaultValueProvider.Empty, .Mock, or a custom provider)
- If you want 'no customization', assign DefaultValueProvider.Empty rather than null
- Null-check the source value before copying it into the property
Example fix
// before factory.DefaultValueProvider = maybeNull; // after factory.DefaultValueProvider = maybeNull ?? DefaultValueProvider.Empty;
Defensive patterns
Strategy: validation
Validate before calling
if (provider is null)
provider = DefaultValueProvider.Empty;
factory.DefaultValueProvider = provider; Try / catch
try
{
factory.DefaultValueProvider = provider;
}
catch (ArgumentNullException ex)
{
// ex.ParamName == "value"
factory.DefaultValueProvider = DefaultValueProvider.Empty;
} Prevention
- Null-coalesce nullable sources before assigning
- Never copy properties from uninitialized factory/config objects without checks
- Use DefaultValueProvider.Empty to express 'default' instead of null
When it happens
Trigger: Executing factory.DefaultValueProvider = null, typically via a property copy from a nullable source, reflection, or a config object whose field is null.
Common situations: Copying settings between MockFactory instances where the source was never initialized; data binding that pushes null into the property.
Related errors
- callback (Argument is null)
- value (Argument is out of range)
- Unsupported expression
- Could not determine the correct positions for all argument…
- The return type of the last member shown above is not…
AI-assisted analysis of devlooped/moq@89a5be629c (2026-09-16).
Data as JSON: /api/errors/23803c4f02d2b385.
Report an issue: GitHub.
Appendix: source
Thrown at src/Moq/Obsolete/MockFactory.cs:161
set
{
this.DefaultValueProvider = value switch
{
DefaultValue.Empty => DefaultValueProvider.Empty,
DefaultValue.Mock => DefaultValueProvider.Mock,
_ => throw new ArgumentOutOfRangeException(nameof(value)),
};
}
}
/// <summary>
/// Gets or sets the <see cref="Moq.DefaultValueProvider"/> instance that will be used
/// e. g. to produce default return values for unexpected invocations.
/// </summary>
public DefaultValueProvider DefaultValueProvider
{
get => this.defaultValueProvider;
set => this.defaultValueProvider = value ?? throw new ArgumentNullException(nameof(value));
}
/// <summary>
/// Gets the mocks that have been created by this factory and
/// that will get verified together.
/// </summary>
protected internal IEnumerable<Mock> Mocks { get { return mocks; } }
/// <summary>
/// A set of switches that influence how mocks created by this factory will operate.
/// You can opt in or out of certain features via this property.
/// </summary>
public Switches Switches
{
get => this.switches;
set => this.switches = value;
}
View on GitHub (pinned to 89a5be629c)