devlooped/moq · error · ArgumentOutOfRangeException
value (Argument is out of range)
Error message
value (Argument is out of range)
What it means
Moq's Mock<T>.DefaultValue property setter validates that the assigned enum value is one of the supported DefaultValue members (Empty or Mock). Any other value (invalid cast, undefined enum constant, DefaultValue.CallBase legacy value) throws ArgumentOutOfRangeException, because Moq has no DefaultValueProvider mapping for it.
Solutions
- Only assign DefaultValue.Empty or DefaultValue.Mock to Mock.DefaultValue
- If you need custom default behavior, set mock.DefaultValueProvider to a DefaultValueProvider instance (including custom ones) instead of using the enum
- Remove any integer casts or config-driven enum parsing that can produce undefined DefaultValue values
- Verify the Moq version and update code that references removed enum members like CallBase
Example fix
// before mock.DefaultValue = (DefaultValue)5; // throws ArgumentOutOfRangeException // after mock.DefaultValue = DefaultValue.Mock; // or: mock.DefaultValueProvider = DefaultValueProvider.Mock;
Defensive patterns
Strategy: validation
Validate before calling
static bool IsValidDefaultValue(DefaultValue v) => v is DefaultValue.Empty or DefaultValue.Mock; // guard before assignment: // if (!IsValidDefaultValue(v)) throw new ArgumentOutOfRangeException(nameof(v));
Type guard
static bool IsDefinedDefaultValue(object? v) => v is DefaultValue d && d is DefaultValue.Empty or DefaultValue.Mock;
Prevention
- Never cast arbitrary ints to the DefaultValue enum
- Prefer setting mock.DefaultValueProvider directly with DefaultValueProvider.Empty/Mock/custom providers
- Check Moq release notes when upgrading for enum member changes
When it happens
Trigger: Assigning an invalid DefaultValue enum value to mock.DefaultValue, e.g. mock.DefaultValue = (DefaultValue)99 or a value from an older/newer Moq version that no longer exists in the enum. The switch only maps DefaultValue.Empty and DefaultValue.Mock; everything else falls to the throw.
Common situations: Casting raw ints to DefaultValue when configuring mocks dynamically or via config files; upgrading/downgrading Moq where the enum lost or gained members (e.g. obsolete DefaultValue.CallBase); copy-pasted code using a different library's DefaultValue enum.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- Delays have to be greater than zero to ensure an async…
- value (Argument is out of range)
- ArgumentOutOfRangeException: callCount
- ArgumentOutOfRangeException: callCount
- ArgumentOutOfRangeException: callCountFrom (invalid range…
AI-assisted analysis of devlooped/moq@89a5be629c (2026-09-16).
Data as JSON: /api/errors/5c8cc310f836db26.
Report an issue: GitHub.
Appendix: source
Thrown at src/Moq/Mock.cs:171
internal abstract object?[] ConstructorArguments { get; }
/// <summary>
/// Specifies the behavior to use when returning default values for unexpected invocations on loose mocks.
/// </summary>
public DefaultValue DefaultValue
{
get
{
return this.DefaultValueProvider.Kind;
}
set
{
this.DefaultValueProvider = value switch
{
DefaultValue.Empty => DefaultValueProvider.Empty,
DefaultValue.Mock => DefaultValueProvider.Mock,
_ => throw new ArgumentOutOfRangeException(nameof(value)),
};
}
}
internal abstract EventHandlerCollection EventHandlers { get; }
/// <summary>
/// Gets the mocked object instance.
/// </summary>
public object Object => this.OnGetObject();
/// <summary>
/// Gets the interfaces directly inherited from the mocked type (<see cref="MockedType"/>).
/// </summary>
internal abstract Type[] InheritedInterfaces { get; }
internal abstract bool IsObjectInitialized { get; }
View on GitHub (pinned to 89a5be629c)