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.

`Should().BeEquivalentTo()` show different error message between Debug and Release

See original GitHub issue

Description

We compile and launch unit test libraries with the release configuration and x64 platform. After upgrading FluentAssertions from 4.19.2 to 6.5.1 (the latest), we noticed that Should().BeEquivalentTo() gives different error messages while compare classes with properties. Let’s look at the following example:

  • Let ClassWithProperty be a class containing one property IntProperty:
public sealed class ClassWithProperty
{
    public ClassWithProperty(int value)
    {
        IntProperty = value;
    }
    public int IntProperty { get; }
}
  • The unit test CompareClassWithPropertyObjects compare two objects of type ClassWithProperty, with different property values.
[Test]
public void CompareClassWithPropertyObjects()
{
   //Arrange
    var actual = new ClassWithProperty(1);
    var expected = new ClassWithProperty(2);
    
    //Assert
    actual.Should().BeEquivalentTo(expected);
}

Expected behavior:

Expected property actual.IntProperty to be 2, but found 1.

With configuration:
- Use declared types and members
- Compare enums by value
- Compare tuples by their properties
- Compare anonymous types by their properties
- Compare records by their members
- Match member by name (or throw)
- Be strict about the order of items in byte arrays
- Without automatic conversion.

Actual behavior:

Here, it outputs the }}}, instead of the name of the actual object.

Expected property }}}.IntProperty to be 2, but found 1.

With configuration:
- Use declared types and members
- Compare enums by value
- Compare tuples by their properties
- Compare anonymous types by their properties
- Compare records by their members
- Match member by name (or throw)
- Be strict about the order of items in byte arrays
- Without automatic conversion.

Versions

  • .NET : .NET Core 3.1 and .NET 4.7.1
  • NUnit : 3.12.0 (I tried also the the version 3.13.1, It gives the same results).
  • FluentAssertions : 6.5.1
  • Configuration : Release
  • Platform : x64

Note that it works totally fine in Debug configuration.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
dennisdoomencommented, Mar 19, 2022

As is documented here, you need to run your tests in Debug mode for FA to be able to extract the variable from the stack trace. Otherwise the compiler will optimize those details away. However, it should fall back to using a generic name, so technically it’s a little bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FluentAssertions Should().BeEquivalentTo() fails in trivial ...
So I think Should().BeEquivalentTo is seeing that your objects implement their own equality, calling into (presumably) AddressDto.
Read more >
Fluent Assertions 10 Top Tips
Fluent Assertions has a really useful extension method BeEquivalentTo() . This allows object graph comparison and therefore removes the need to ...
Read more >
Fixing the 'Undefined Reference' Errors for C/C++ Projects
This tutorial shows how to various problems related to missing symbols in C/C++ projects. We will create a basic project calling the ...
Read more >
How To Use Classes in TypeScript
This tutorial will go through the syntax used to create classes, the different features available, and how classes are treated in TypeScript ...
Read more >
9 Fluent Assertions Tricks to Save Hours of Your Testing ...
Should().Be(5, "because that is the correct amount");. When the above assert fails, the following error message will be displayed in the Test output...
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