devlooped/moq · error · NotSupportedException
LINQ method ' ' not supported.
Error message
LINQ method '{0}' not supported. What it means
When building mock setups from a LINQ query expression, MockSetupsBuilder.VisitMethodCall rejects calls to LINQ methods listed in its unsupportedMethods set (e.g. Count(), Contains(), Where(), Select(), First(), Any(), Skip(), Take()). Such method calls cannot be translated into a single mock setup, so Moq throws NotSupportedException with 'LINQ method {0} not supported.'
Solutions
- Replace the LINQ method with a property or indexer comparison the provider understands (e.g. f.Items.Length == 2 for arrays).
- Use the standard setup API: mock.Setup(f => f.Items).Returns(items) and let the code under test exercise the real LINQ operators on the returned collection.
- Express membership with element equality (f.Items[0] == x) or restructure the specification to avoid Contains/Any/Count inside the query.
- Use It.Is<T>(predicate) matchers in mock.Setup for complex conditions.
Example fix
// before
var foo = Mock.Of<IFoo>(f => f.Items.Count() == 2);
// after
var mock = new Mock<IFoo>();
mock.Setup(f => f.Items).Returns(new[] { item1, item2 }); Defensive patterns
Strategy: validation
Validate before calling
// Do not call LINQ operators (Count, Contains, Any, Where, First, ...) inside Mock.Of specifications. // Validate: predicate body should only contain property/indexer access and ==/&&.
Try / catch
try { var foo = Mock.Of<IFoo>(f => f.Items.Count() == 2); }
catch (NotSupportedException) { var mock = new Mock<IFoo>(); mock.Setup(f => f.Items).Returns(items); foo = mock.Object; } Prevention
- Return fully-populated collections from mock.Setup(...).Returns(...) instead of asserting on collection LINQ operators.
- Use It.Is<T>(x => x.Count() == 2) as a matcher argument, not inside query syntax.
- Treat Mock.Of as an exact-property-shape tool only; anything involving LINQ methods belongs in the setup API.
When it happens
Trigger: Mock.Of<IFoo>(f => f.Items.Count() == 2) or f => f.List.Contains(x) or f => f.Children.Any(c => c.X) — VisitMethodCall sees node.Method.Name in the unsupported list and throws NotSupportedException.
Common situations: Trying to express collection-shape expectations in query syntax; checking Count()/Any() on mocked collection properties in Mock.Of; copying LINQ-to-Objects predicates into LINQ-to-Mocks specifications.
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
- The equals ("==" or "=" in VB) and the conditional 'and'…
- Type to mock ( ) must be an interface, a delegate, or a…
- A matching constructor for the given arguments was not…
- It is impossible to call the provided strongly-typed…
- ArgumentNullException: Value cannot be null. (Parameter…
AI-assisted analysis of devlooped/moq@89a5be629c (2026-09-16).
Data as JSON: /api/errors/e709645dc9484939.
Report an issue: GitHub.
Appendix: source
Thrown at src/Moq/Linq/MockSetupsBuilder.cs:74
return ConvertToSetupReturns(node, Expression.Constant(true));
}
return base.VisitMember(node);
}
protected override Expression VisitMethodCall(MethodCallExpression node)
{
if (node.Method.DeclaringType == typeof(Queryable) && queryableMethods.Contains(node.Method.Name))
{
this.stackIndex++;
var result = base.VisitMethodCall(node);
this.stackIndex--;
return result;
}
if (unsupportedMethods.Contains(node.Method.Name))
{
throw new NotSupportedException(string.Format(
CultureInfo.CurrentCulture,
Resources.LinqMethodNotSupported,
node.Method.Name));
}
if (this.stackIndex > 0 && node.Type == typeof(bool))
{
return ConvertToSetupReturns(node, Expression.Constant(true));
}
return base.VisitMethodCall(node);
}
protected override Expression VisitUnary(UnaryExpression node)
{
if (this.stackIndex > 0 && node.NodeType == ExpressionType.Not)
{
return ConvertToSetup(node.Operand, Expression.Constant(false)) ?? base.VisitUnary(node);View on GitHub (pinned to 89a5be629c)