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

  1. Replace the LINQ method with a property or indexer comparison the provider understands (e.g. f.Items.Length == 2 for arrays).
  2. 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.
  3. Express membership with element equality (f.Items[0] == x) or restructure the specification to avoid Contains/Any/Count inside the query.
  4. 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

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


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)