Search code examples
c#unit-testingfluent-assertions

Fluent Assertions: Be() vs Equals()


What is the difference between:

  • subject.Should().Be(expected)
  • subject.Should().Equals(expected)

I always use Be(), but I now have a testcase where Be() gives a different result, then Equals(). My testcase involves a struct and comparing it with the default of that struct.

MyStruct subject = new MyStruct("value");

Assert.Equal(default, subject);                  // xUnit Assert => works
Assert.Equal(default(MyStruct), (object)subject); // xUnit Assert => works
subject.Should().Equals(default);                // works
subject.Should().Be(default(MyStruct));          // FAILS!

Are Be() and Equal() the same and is this a bug in Fluent Assertions? Or are they asserting different things?


Solution

  • The Equals method comes from System.Object.
    You actually compare an object of Type ObjectAssertions with the default value of object(null).

    This is not an assertion.
    The method returns a boolean that is false.

    enter image description here