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.

BeEquivalentTo throws a NullReferenceException

See original GitHub issue

Description

while running tests in parallel ( 2 threads) and when using BeEquivalentTo() to compare objects from two lists a null reference exception is thrown. However no NRE is thrown on using single thread

System.NullReferenceException : Object reference not set to an instance of an object.
   at FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions`1.ToString()
public class Option
{
    public string Id { get; set; }
    public string Name { get; set; }
}
List<Option> expectedOptions;
List<Option> actualOptions;

Complete minimal example reproducing the issue

// Arrange
List<Option> expectedOptions = new List<Option>
{
    new Option()
    {
        Id = "4b71bcd9",
        Name = "AutoTest_123"
    }
};

// Act
List<Option> actualOptions = new List<Option>
{
    new Option()
    {
        Id = "4b71bcd9",
        Name = "AutoTest_123"
    }
};

// Assert
for (var i = 0; i < expectedOptions.Count; i++)
{
    expectedOptions [i].Should().BeEquivalentTo(actualOptions[i]);
}

Expected behavior:

The assertion should be true.

Actual behavior:

System.NullReferenceException : Object reference not set to an instance of an object.
   at FluentAssertions.Equivalency.SelfReferenceEquivalencyAssertionOptions`1.ToString()

Versions

FluentAssertions Version=“6.7.0”

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:16 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
jnyrupcommented, Jul 4, 2022

Thanks for the complete example, it made it a lot easier to diagnose.

ApiTestHelper.GlobalSetUp is invoked concurrently for both tests and does call AssertionOptions.AssertEquivalencyUsing which is not safe.

One way to solve it is to move the setup of Fluent Assertions into a Module Initializer which are guaranteed to only run once.

[System.Runtime.CompilerServices.ModuleInitializer]
internal static void SetupFluentAssertions()
{
    AssertionOptions.AssertEquivalencyUsing(options => options
        .Using<DateTimeOffset>(ctx => ctx.Subject.Should().BeSameDateAs(ctx.Expectation))
        .WhenTypeIs<DateTimeOffset>()
        .Using<DateTime>(ctx => ctx.Subject.Should().BeSameDateAs(ctx.Expectation))
        .WhenTypeIs<DateTime>()
    );
}
1reaction
DenisSokolcommented, May 25, 2022

@jnyrup, Thanks a lot for digging into the problem resolving. I appreciate your help. It works now!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ShouldBeEquivalentTo causing object reference not set ...
Issue 1 has been resolved in the upcoming v3.0 (now in alpha) and was caused by a null object. Issue 2 can be...
Read more >
Upgrading to version 6.0 - Fluent Assertions
An enum is now only considered to be equivalent to an enum of the same or another type, but you can control whether...
Read more >
Improving Unit Tests with Fluent Assertions
We will assert that the Customer class' GetId() method will throw a NullReferenceException if the Id property is null :.
Read more >
Eh... Am I being trolled? How can this be null? : r/csharp
However, when you call the class member, the property called Id , and the owner object reference is not set (is null ),...
Read more >
9 Fluent Assertions Tricks to Save Hours of Your Testing ...
BeEquivalentTo method compares properties and it requires that properties have the same names, no matter the actual type of the properties. Fluent Assertions ......
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