devlooped/moq · error · ArgumentException
Value cannot be an empty string.
Error message
Value cannot be an empty string.
What it means
Guard.NotNullOrEmpty validates that a string argument is non-null and non-empty before Moq proceeds. After the null check throws ArgumentNullException, an empty string (Length == 0) throws ArgumentException with "Value cannot be an empty string.". It protects APIs that require a meaningful string identifier (e.g. member or event names).
Solutions
- Pass a non-empty string value (trim user/config input before calling).
- Default the value to a valid fallback when it would be empty.
- If null vs empty semantics differ, ensure the caller passes null (which throws ArgumentNullException instead) only when the API allows it.
- Validate inputs at your API boundary before invoking Moq.
Example fix
// before
string name = config.MemberName ?? ""; // may be ""
moqHelper.Invoke(name); // throws
// after
string name = config.MemberName;
if (string.IsNullOrEmpty(name)) throw new InvalidOperationException("MemberName not configured");
moqHelper.Invoke(name); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrEmpty(name)) throw new InvalidOperationException("Name must be non-empty before calling the API"); Type guard
static string RequireNonEmpty(string? s, [CallerArgumentExpression(nameof(s))] string? p = null) => string.IsNullOrEmpty(s) ? throw new ArgumentException("Value cannot be empty", p) : s; Try / catch
try { api.Invoke(name); }
catch (ArgumentException ex) when (ex.Message == "Value cannot be an empty string.") { /* supply a valid name */ throw; } Prevention
- Validate/trim string inputs at your boundary before invoking library APIs.
- Never default string identifiers to string.Empty; use null-with-check or a valid sentinel.
- Add unit tests covering empty-string inputs to your configuration paths.
When it happens
Trigger: Passing string.Empty to any Moq API routed through this guard, e.g. empty property/member names in weak-event or reflection-based helpers, empty event names in Raise/Event setups.
Common situations: Building names dynamically from config or reflection and getting an empty result; default-initialized string fields; copy-paste leaving a placeholder empty.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- Unsupported expression
- Could not determine the correct positions for all argument…
- The return type of the last member shown above is not…
- Value cannot be null. (Parameter 'value')
- Unsupported expression
AI-assisted analysis of devlooped/moq@89a5be629c (2026-09-16).
Data as JSON: /api/errors/511bcc1679601aba.
Report an issue: GitHub.
Appendix: source
Thrown at src/Moq/Guard.cs:195
throw new ArgumentNullException(paramName);
}
}
/// <summary>
/// Ensures the given string <paramref name="value"/> is not null or empty.
/// Throws <see cref="ArgumentNullException"/> in the first case, or
/// <see cref="ArgumentException"/> in the latter.
/// </summary>
public static void NotNullOrEmpty(string value, string paramName)
{
if (value == null)
{
throw new ArgumentNullException(paramName);
}
if (value.Length == 0)
{
throw new ArgumentException(Resources.ArgumentCannotBeEmpty, paramName);
}
}
public static void NotField(MemberExpression memberAccess)
{
if (memberAccess.Member is FieldInfo)
throw new NotSupportedException(
string.Format(
Resources.FieldsNotSupported,
memberAccess.ToStringFixed()));
}
public static void IsMockable(Type type)
{
if (!type.IsMockable())
{
throw new NotSupportedException(
string.Format(View on GitHub (pinned to 89a5be629c)