Skip to content

Satisfy doesn't support extension methods #1688

@Bouke

Description

@Bouke

Description

Calling an extension method from the expression provided to Satisfy causes an exception. I think this shouldn't happen and calling extension methods should work.

Complete minimal example reproducing the issue

// Fails: IEnumerable<int>.Contains is an extension method
var actual = new[] { 1, 2, 3 };
var allowed = new[] { 1, 2, 3 };
actual.Should().Satisfy(
	x => allowed.Contains(x),
	x => allowed.Contains(x),
	x => allowed.Contains(x));

// Works: HashSet<int>.Contains is not an extension method
var actual = new[] { 1, 2, 3 };
var allowed = new HashSet<int> { 1, 2, 3 };
actual.Should().Satisfy(
	x => allowed.Contains(x),
	x => allowed.Contains(x),
	x => allowed.Contains(x));

Expected behavior:

Should work.

Actual behavior:

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'body')
   at System.Linq.Expressions.Expression.Lambda(Expression body, String name, Boolean tailCall, IEnumerable`1 parameters)
   at System.Linq.Expressions.Expression.Lambda(Expression body, ParameterExpression[] parameters)
   at FluentAssertions.Formatting.PredicateLambdaExpressionValueFormatter.ConstantSubExpressionReductionVisitor.Visit(Expression node) in /_/Src/FluentAssertions/Formatting/PredicateLambdaExpressionValueFormatter.cs:line 86
   at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at FluentAssertions.Formatting.PredicateLambdaExpressionValueFormatter.ConstantSubExpressionReductionVisitor.Visit(Expression node) in /_/Src/FluentAssertions/Formatting/PredicateLambdaExpressionValueFormatter.cs:line 89
   at FluentAssertions.Formatting.PredicateLambdaExpressionValueFormatter.ReduceConstantSubExpressions(Expression expression) in /_/Src/FluentAssertions/Formatting/PredicateLambdaExpressionValueFormatter.cs:line 40
   at FluentAssertions.Formatting.PredicateLambdaExpressionValueFormatter.Format(Object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild) in /_/Src/FluentAssertions/Formatting/PredicateLambdaExpressionValueFormatter.cs:line 17
   at FluentAssertions.Formatting.Formatter.Format(Object value, FormattedObjectGraph output, FormattingContext context, FormatChild formatChild) in /_/Src/FluentAssertions/Formatting/Formatter.cs:line 153
   at FluentAssertions.Formatting.Formatter.ToString(Object value, FormattingOptions options) in /_/Src/FluentAssertions/Formatting/Formatter.cs:line 106
   at FluentAssertions.Collections.GenericCollectionAssertions`3.<>c.<Satisfy>b__85_3(Predicate`1 predicate) in /_/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs:line 2832
   at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
   at System.String.Join(String separator, IEnumerable`1 values)
   at FluentAssertions.Collections.GenericCollectionAssertions`3.Satisfy(IEnumerable`1 predicates, String because, Object[] becauseArgs) in /_/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs:line 2830
   at FluentAssertions.Collections.GenericCollectionAssertions`3.Satisfy(Expression`1[] predicates) in /_/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs:line 2777
   at Program.Main()

Versions

  • Which version of Fluent Assertions are you using? 6.1
  • Which .NET runtime and version are you targeting? .NET 5

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions