devlooped/moq · error · ArgumentNullException
condition (Argument is null)
Error message
condition (Argument is null)
What it means
Moq throws this ArgumentNullException when a null condition delegate is passed to Mock<T>.When(Func<bool> condition). The condition is evaluated to decide whether a conditional setup applies, so a null predicate is invalid.
Solutions
- Pass a real predicate: mock.When(() => someState).Setup(...).
- Null-check condition factory results before passing them to When.
- If no condition is needed, use plain Setup instead of When.
Example fix
// before mock.When(null).Setup(m => m.Foo()).Returns(1); // after mock.When(() => isEnabled).Setup(m => m.Foo()).Returns(1);
Defensive patterns
Strategy: type-guard
Validate before calling
if (condition is null) throw new ArgumentNullException(nameof(condition));
Type guard
bool IsValidCondition(Func<bool> c) => c is not null;
Try / catch
try { mock.When(condition).Setup(...); } catch (ArgumentNullException) { mock.Setup(...); } Prevention
- Null-check predicates returned from condition factories
- Prefer inline lambdas over nullable Func variables
- Use plain Setup when no condition is required
When it happens
Trigger: mock.When(null) — any call to When whose condition argument is null (src/Moq/Mock`1.cs:690).
Common situations: Storing the predicate in a variable that is never assigned or returned null from a condition-factory; refactoring that removed the lambda body but left a null literal.
Related errors
- Value cannot be null. (Parameter 'value')
- Value cannot be null. (Parameter 'value')
- Value cannot be null. (Parameter 'newExpression')
- callback (Argument is null)
- value (Argument is null)
AI-assisted analysis of devlooped/moq@89a5be629c (2026-09-16).
Data as JSON: /api/errors/f9a5aa5b2a2f18e3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Moq/Mock`1.cs:690
#endregion
#region When
/// <summary>
/// Allows setting up a conditional setup.
/// Conditional setups are only matched by an invocation
/// when the specified condition evaluates to <see langword="true"/>
/// at the time when the invocation occurs.
/// </summary>
/// <param name="condition">
/// The condition that should be checked
/// when a setup is being matched against an invocation.
/// </param>
public ISetupConditionResult<T> When(Func<bool> condition)
{
if (condition == null)
{
throw new ArgumentNullException(nameof(condition));
}
return new WhenPhrase<T>(this, new Condition(condition));
}
#endregion
#region Verify
/// <summary>
/// Verifies that a specific invocation matching the given expression was performed on the mock.
/// Use in conjunction with the default <see cref="MockBehavior.Loose"/>.
/// </summary>
/// <param name="expression">Expression to verify.</param>
/// <exception cref="MockException">The invocation was not performed on the mock.</exception>
/// <example group="verification">
/// This example assumes that the mock has been used, and later we want to verify
/// that a given invocation with specific parameters was performed:View on GitHub (pinned to 89a5be629c)