devlooped/moq · error · NotSupportedException
Member is not supported for protected mocking.
Error message
Member {0} is not supported for protected mocking. What it means
When converting argument-match expressions to types, Moq only understands MemberExpression members that are methods (for the parameter types) or properties (for their property type). Any other member kind (e.g. fields, events, indexers) used in a matcher is not supported for protected mocking and triggers this NotSupportedException.
Solutions
- Rewrite the matcher so it references a property or method, or use a simple constant/ItExpr matcher instead
- Compute the value into a local variable before the setup and match with ItExpr.Is<T>(v => v == local) without member access
- Use a plain value or ItExpr.IsAny<T>() if member-specific matching is unnecessary
Example fix
// before ItExpr.Is<Customer>(c => c.Name == "x") // Name is a field // after ItExpr.Is<Customer>(c => c.GetDisplayName() == "x") // method member, or make Name a property
Defensive patterns
Strategy: validation
Validate before calling
// ensure matcher member access targets properties/methods, not fields
class Customer { public string Name => _name; } // property, not public field Try / catch
try { /* protected setup with member matcher */ } catch (NotSupportedException ex) { /* rewrite matcher with property/method or local constant */ } Prevention
- Avoid field access inside ItExpr.Is predicates
- Match on pre-computed locals instead of member expressions
- Keep protected-mock matchers simple: constants, ItExpr.IsAny, ItExpr.IsNull
When it happens
Trigger: Using an ItExpr.Is<T>(x => x.SomeField ...) style matcher whose expression references a field or other non-method/non-property member, causing member.Member to be neither MethodInfo nor PropertyInfo.
Common situations: Matching on a public field in a predicate; porting a regular Moq It.Is matcher to the Protected/ItExpr API where fewer expression shapes are supported.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Out expression must evaluate to a constant value.
- The return type of the last member shown above is not…
- No constructor call could be found.
- Type to mock ( ) must be an interface, a delegate, or a…
- Type to mock ( ) must be an interface, a delegate, or a…
AI-assisted analysis of devlooped/moq@89a5be629c (2026-09-16).
Data as JSON: /api/errors/04a76be8710ab5c6.
Report an issue: GitHub.
Appendix: source
Thrown at src/Moq/Protected/ProtectedMock.cs:495
}
else if (ItRefAnyField(expr) is FieldInfo itRefAnyField)
{
types[index] = itRefAnyField.FieldType.MakeByRefType();
}
else if (expr.NodeType == ExpressionType.MemberAccess)
{
var member = (MemberExpression)expr;
if (member.Member is FieldInfo field)
{
types[index] = field.FieldType;
}
else if (member.Member is PropertyInfo property)
{
types[index] = property.PropertyType;
}
else
{
throw new NotSupportedException(string.Format(
Resources.Culture,
Resources.UnsupportedMember,
member.Member.Name));
}
}
else
{
types[index] = (expr.PartialEval() as ConstantExpression)?.Type;
}
}
return types;
}
static bool IsItRefAny(Expression expression)
{
return ItRefAnyField(expression) != null;
}View on GitHub (pinned to 89a5be629c)