fluentassertions: InvalidOperationException when asserting equivalent with default instance of ImmutableArray

Description

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

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 26 (22 by maintainers)

Most upvoted comments

catch (InvalidOperationException) when (value.GetType().Name.StartsWith(“ImutableArray`”))

Hmm, on second thought, maybe we should just bite the bullet and accept taking a dependency on the System.Collections.Immutable package.

Or maybe make it a separate NuGet package?

I can remember somebody suggestion such a solution once… https://youtu.be/hswLqvv3kdw?t=2476