`Should().BeEquivalentTo()` show different error message between Debug and Release
See original GitHub issueDescription
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 propertyIntProperty
:
public sealed class ClassWithProperty
{
public ClassWithProperty(int value)
{
IntProperty = value;
}
public int IntProperty { get; }
}
- The unit test
CompareClassWithPropertyObjects
compare two objects of typeClassWithProperty
, 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 version3.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:
- Created 2 years ago
- Comments:12 (12 by maintainers)
Top 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 >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
I did a related analysis in https://github.com/fluentassertions/fluentassertions/issues/1781#issuecomment-1016784748
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.