devlooped/moq · error · ArgumentException
Member . does not exist.
Error message
Member {0}.{1} does not exist. What it means
ProtectedMock.ProtectedAs<T>... string-name based APIs (Setup/SetupGet/SetupSet/Verify with a member name string) look up the member on the mocked type T; ThrowIfMemberMissing throws ArgumentException with Resources.MemberMissing ('Member {0}.{1} does not exist.') when the reflection lookup returns null. It means the named member simply is not present on T with the requested accessibility.
Solutions
- Correct the member name string, using nameof where possible
- Check the member's actual declared accessibility and name via reflection or the class source
- Switch to mock.Protected().As<TAnalog>() or strong-typed Mock<T> setups to get compile-time checking
- Update the name after upgrading the mocked library version
Example fix
// before
mock.Protected().Setup<int>("GetValu");
// after
mock.Protected().Setup<int>("GetValue"); // member actually named GetValue on T Defensive patterns
Strategy: validation
Validate before calling
static bool MemberExists(Type target, string name) =>
target.GetMember(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).Length > 0; Try / catch
try { mock.Protected().Setup<int>("GetValue"); }
catch (ArgumentException ex) when (ex.Message.Contains("does not exist")) { /* fix member-name string */ } Prevention
- Use nameof or constants for member-name strings to survive renames
- Double-check spelling and accessibility before writing string-based protected setups
- Prefer Protected().As<TAnalog>() for compile-time safety
- After library upgrades, grep tests for renamed member strings
When it happens
Trigger: mock.Protected().Setup(x => x.MethodName(...)) / SetupGet("PropName") / SetupSet("PropName", value) where no method, property, or field with that exact name exists (non-public instance, or public — depending on the lookup overload) on T.
Common situations: Typo in the member-name string; member renamed in a newer library version (string-based API misses compile-time safety); member is actually public and resolved on a different path; member exists only on a base type with unexpected accessibility.
Understand the failure class
Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.
Related errors
- ex.Message (re-thrown ArgumentException with paramName…
- Type does not have matching protected member
- No protected method . found whose signature is compatible…
- "public bool ( ) in class .
- ex.Message from ReplaceDuck expression rewriting (rethrown…
AI-assisted analysis of devlooped/moq@89a5be629c (2026-09-16).
Data as JSON: /api/errors/452d4cee8791d644.
Report an issue: GitHub.
Appendix: source
Thrown at src/Moq/Protected/ProtectedMock.cs:371
static Expression<Action<T>> GetSetterExpression(PropertyInfo property, Expression value)
{
var param = Expression.Parameter(typeof(T), "mock");
return Expression.Lambda<Action<T>>(
Expression.Call(param, property.GetSetMethod(true), value),
param);
}
#if NULLABLE_REFERENCE_TYPES
static void ThrowIfMemberMissing(string memberName, [NotNull] MemberInfo? member)
#else
static void ThrowIfMemberMissing(string memberName, MemberInfo? member)
#endif
{
if (member == null)
{
throw new ArgumentException(string.Format(
CultureInfo.CurrentCulture,
Resources.MemberMissing,
typeof(T).Name,
memberName));
}
}
#if NULLABLE_REFERENCE_TYPES
static void ThrowIfMethodMissing(string methodName, [NotNull] MethodInfo? method, object[] args)
#else
static void ThrowIfMethodMissing(string methodName, MethodInfo? method, object[] args)
#endif
{
if (method == null)
{
List<string> extractedTypeNames = new List<string>();
foreach (object o in args)
{View on GitHub (pinned to 89a5be629c)