InvalidOperationException when asserting equivalent with default instance of ImmutableArray<T>
See original GitHub issueDescription
When comparing an ImmutableArray<T>
against its default value using Should().BeEquivalentTo(...)
an exception is thrown by the attempt to examine the array as a collection.
Complete minimal example reproducing the issue
The following shows how to produce the error:
default(ImmutableArray<string>).Should().BeEquivalentTo(default(ImmutableArray<string>));
Comparing empty arrays does not cause the issue:
ImmutableArray<string>.Empty.Should().BeEquivalentTo(ImmutableArray<string>.Empty);
Expected behavior:
I expect the comparison to succeed without an exception. In the case that a non-default array is compared against a default array, I expect an exception to be thrown highlighting the difference between expected and actual values.
Actual behavior:
The following exception is thrown:
Message:
System.InvalidOperationException : This operation cannot be performed on a default instance of ImmutableArray<T>. Consider initializing the array, or checking the ImmutableArray<T>.IsDefault property.
Stack Trace:
ImmutableArray`1.ThrowInvalidOperationIfNotInitialized()
ICollection<T>.get_Count()
EnumerableHelpers.ToArray[T](IEnumerable`1 source)
GenericEnumerableEquivalencyStep.HandleImpl[T](EnumerableEquivalencyValidator validator, Object[] subject, IEnumerable`1 expectation)
--- End of stack trace from previous location ---
GenericEnumerableEquivalencyStep.Handle(Comparands comparands, IEquivalencyValidationContext context, IEquivalencyValidator nestedValidator)
EquivalencyValidator.RunStepsUntilEquivalencyIsProven(Comparands comparands, IEquivalencyValidationContext context)
EquivalencyValidator.RecursivelyAssertEquality(Comparands comparands, IEquivalencyValidationContext context)
EquivalencyValidator.AssertEquality(Comparands comparands, EquivalencyValidationContext context)
StringCollectionAssertions`2.BeEquivalentTo(IEnumerable`1 expectation, Func`2 config, String because, Object[] becauseArgs)
StringCollectionAssertions`2.BeEquivalentTo(IEnumerable`1 expectation, String because, Object[] becauseArgs)
Versions
Fluent Assertions version: 6.0.0 .NET Target version: net5.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:26 (22 by maintainers)
Top Results From Across the Web
Do not use default constructor of ImmutableArray<T> #34096
By calling the default constructor the underlying field storing the array is initialized to null . For performance reason it's not viable to ......
Read more >IsEquivalent/Is.Not.Empty on default ImmutableArray ...
The following test fails with an InvalidOperationException: This operation cannot be performed on a default instance of ImmutableArray.
Read more >Releases - Fluent Assertions
Prevent exceptions when asserting on ImmutableArray<T> - #1668 ... able to assert that all strings in collection are equal to the specified string...
Read more >Checking the .NET Core Libraries Source Code by the PVS ...
As ImmutableArray<T> is a structure, it has a default constructor (without arguments) that will result in the array field taking value by default,...
Read more >Performance Improvements in .NET 7
NET 7 is fast. Really fast. This post deep-dives into hundreds of performance improvements that contributed to that reality.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Or maybe make it a separate NuGet package?
I can remember somebody suggestion such a solution once… https://youtu.be/hswLqvv3kdw?t=2476
Let’s remain on a conservative path and accept the hack for now.