简体   繁体   中英

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?

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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM