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.

Be() and BeSameAs() are doing the same thing

See original GitHub issue

Description

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.

Documentation:

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:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jnyrupcommented, Jan 15, 2020

Be uses object.Equals() to compare whether your two identical instances ProductRequestModel are to be considered equal. As you haven’t overridden object.Equals() on ProductRequestModel your objects are compared for reference equality, which is why you experience identical behavior as BeSameAs.

To compare the two objects memberwise, you can use BeEquivalentTo.

product1.Should().BeEquivalentTo(product2);
0reactions
dennisdoomencommented, Jan 16, 2020

I did some further research and discovered that apparently this happens due to C# own design of Object.Equals(). As stated here, it seems that the method in question gets overridden by anonymous types, “so that two instances of the same anonymous type are equal if and only if all their properties are equal”.

Yep. That’s why we have two methods. Sometimes people override Equals on a subset of the properties, so just using Be might not what you want. Just like you sometimes use Equals, == or ReferenceEquals for reasons.

I’m closing this issue since FA does exactly how it is documented and designed.

Read more comments on GitHub >

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

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