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.

InvalidOperationException when asserting equivalent with default instance of ImmutableArray<T>

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
eNeRGy164commented, Aug 30, 2021

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

0reactions
dennisdoomencommented, Sep 10, 2021

Let’s remain on a conservative path and accept the hack for now.

Read more comments on GitHub >

github_iconTop 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 >

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