You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The "parameterless" variant of ContainSingle() (without a predicate) evaluates IEnumerables twice. This means any side-effects in the enumerable chain are executed twice which might lead to unexpected behavior.
using System.Collections.Generic;using System.Linq;using FluentAssertions;vard=newDictionary<int,int>();varxs=new[]{1}.Select(x =>{ d.Add(x, x);// crashes if executed twicereturnx;});
xs.Should().ContainSingle();// crashes//xs.Should().ContainSingle(_ => true); // does not crash
Expected behavior:
xs.Should().ContainSingle(); should not crash.
Actual behavior:
xs.Should().ContainSingle(); crashes.
Versions
FluentAssertions 6.2.0
.NET 6.0
Additional Information
The problem is that ContainSingle() uses Count()here and also SingleOrDefault()here without materializing the enumerable first.
The variant taking a predicate does materialize the enumerable first here.
The text was updated successfully, but these errors were encountered:
Description
The "parameterless" variant of
ContainSingle()
(without a predicate) evaluatesIEnumerable
s twice. This means any side-effects in the enumerable chain are executed twice which might lead to unexpected behavior.Complete minimal example reproducing the issue
https://dotnetfiddle.net/SFGRnc
Expected behavior:
xs.Should().ContainSingle();
should not crash.Actual behavior:
xs.Should().ContainSingle();
crashes.Versions
Additional Information
The problem is that
ContainSingle()
usesCount()
here and alsoSingleOrDefault()
here without materializing the enumerable first.The variant taking a predicate does materialize the enumerable first here.
The text was updated successfully, but these errors were encountered: