Be() and BeSameAs() are doing the same thing
See original GitHub issueDescription
Both methods Be()
and BeSameAs()
are failing when testing equivalent objects in different instances.
As far as I can understand by the documentation only BeSameAs()
should compare if two objects refer to the same object in memory, but actually both functions are doing so.
To assert that two objects are equal (through their implementation of Object.Equals), use
string otherObject = "whatever";
theObject.Should().Be(otherObject, "because they have the same values");
theObject.Should().NotBe(otherObject);
If you want to make sure two objects are not just functionally equal but refer to the exact same object in memory, use the following two methods.
theObject = otherObject;
theObject.Should().BeSameAs(otherObject);
theObject.Should().NotBeSameAs(otherObject);
Complete minimal example reproducing the issue
public class ProductRequestModel
{
public string Name { get; set; }
public bool Active { get; set; }
}
[Fact]
public void Should_Be()
{
var product1 = new ProductRequestModel
{
Active = true,
Name = "Produto",
};
var product2 = new ProductRequestModel
{
Active = true,
Name = "Produto",
};
product1.Should().Be(product2);
}
[Fact]
public void Should_BeSameAS()
{
var product1 = new ProductRequestModel
{
Active = true,
Name = "Produto",
};
var product2 = new ProductRequestModel
{
Active = true,
Name = "Produto",
};
product1.Should().BeSameAs(product2);
}
Expected behavior:
Shoud_Be(), should pass. Shoud_BeSameAs(), should fail.
Actual behavior:
Shoud_Be(), fails Shoud_BeSameAs(), fails
Versions
- Which version of Fluent Assertions are you using? v5.9.0
- Which .NET runtime and version are you targeting? .Net Core 2.2
Additional Information
My error message for Should_Be() is as follows:
Message:
Expected product1 to be
HBSIS.MapaCompetencia.Tests.Integration.ProductRequestModel
{
Active = True
Name = "Produto"
}, but found
HBSIS.MapaCompetencia.Tests.Integration.ProductRequestModel
{
Active = True
Name = "Produto"
}.
The interesting part is that Be(), works as expected when comparing anonymous objects.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Fluent Assertions: Be() vs Equals()
It's worth noting that if you want to assert if an object (e.g. subject in your case) is the same (not by reference...
Read more >Basic Assertions - Fluent Assertions
If you want to make sure two objects are not just functionally equal but refer to the exact same object in memory, use...
Read more >Improving Unit Tests with Fluent Assertions
In this article, we will review many methods Fluent Assertions offers ... The BeEquivalentTo() method behaves the same as Be() but instead ...
Read more >Fluent Assertions - RSSing.com
As explained in this article, floating point variables are inheritably inaccurate and ... BeSameAs() now displays both objects if they are not the...
Read more >How I keep my test names short and functional
By doing that, I can immediately remove some superfluous information from the test name: the fact that we're expecting objects to be “the...
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
Be
usesobject.Equals()
to compare whether your two identical instancesProductRequestModel
are to be considered equal. As you haven’t overriddenobject.Equals()
onProductRequestModel
your objects are compared for reference equality, which is why you experience identical behavior asBeSameAs
.To compare the two objects memberwise, you can use
BeEquivalentTo
.Yep. That’s why we have two methods. Sometimes people override
Equals
on a subset of the properties, so just usingBe
might not what you want. Just like you sometimes useEquals
,==
orReferenceEquals
for reasons.I’m closing this issue since FA does exactly how it is documented and designed.