devlooped/moq · error · ArgumentOutOfRangeException

value (Argument is out of range)

Error message

value (Argument is out of range)

What it means

The obsolete MockFactory DefaultValue setter only accepts DefaultValue.Empty or DefaultValue.Mock; any other enum value throws ArgumentOutOfRangeException with paramName 'value'. The switch expression maps the legacy DefaultValue enum onto the modern DefaultValueProvider, and the discard arm is the explicit rejection of unknown values.

Solutions

  1. Only assign DefaultValue.Empty or DefaultValue.Mock to the factory's DefaultValue property
  2. Validate the enum value before assigning: Enum.IsDefined(typeof(DefaultValue), value)
  3. If you need custom default value behavior, set the DefaultValueProvider property with a DefaultValueProvider instance instead

Example fix

// before
factory.DefaultValue = (DefaultValue)configValue; // throws if not 0/1
// after
if (Enum.IsDefined(typeof(DefaultValue), configValue))
    factory.DefaultValue = (DefaultValue)configValue;
else
    factory.DefaultValueProvider = DefaultValueProvider.Mock;
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(DefaultValue), value))
    throw new ArgumentOutOfRangeException(nameof(value));
factory.DefaultValue = value;

Try / catch

try
{
    factory.DefaultValue = value;
}
catch (ArgumentOutOfRangeException ex)
{
    // ex.ParamName == "value"; fall back to a safe default
    factory.DefaultValueProvider = DefaultValueProvider.Empty;
}

Prevention

When it happens

Trigger: Setting factory.DefaultValue = (DefaultValue)someInt where the int is not 0 (Empty) or 1 (Mock), e.g. an uninitialized/corrupted enum field or a cast from an integer config value.

Common situations: Deserializing configuration into a DefaultValue enum without validating; refactoring that renamed enum members; passing a value from another enum via cast.

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


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

Appendix: source

Thrown at src/Moq/Obsolete/MockFactory.cs:149

        public bool CallBase { get; set; }

        /// <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)),
                };
            }
        }

        /// <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>

View on GitHub (pinned to 89a5be629c)