question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Satisfy doesn't support extension methods

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dennisdoomencommented, Sep 23, 2021

Awesome. Don’t worry. We all do this in our private time 😉

1reaction
vanya-lebedevcommented, Sep 23, 2021

Hi @dennisdoomen. Yes, absolutely. This weekend is already booked. Would next weekend be ok?

Read more comments on GitHub >

github_iconTop Results From Across the Web

C# - using extension methods to provide default interface ...
No. Extension method cannot be used to satisfy interface contracts. – p.s.w.g. Dec 6, 2013 at 5:48.
Read more >
Wrapping Business Logic In Extension Methods For POCOs
Extension methods are Bad(tm). Static methods cant be mocked out. Sure you can test this method, but can you unit test something that...
Read more >
Fluent: Static Extension Methods for Java
Extension methods in a language that explicitly supports them and with good IDE support are perfectly fine. I love them in Kotlin. Extension ......
Read more >
Mocking extension methods used on a mocked object #620
Better to just admit upfront that extension methods, like any other non-virtual or sealed method, simply cannot be mocked at all.
Read more >
Creative use of extension methods in C# - DEV Community
Extension methods don't have that problem because they can't access model's private members and, by nature, aren't part of the model itself. So ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found