4

I have a class, let's call it Foo, that is a value type and hence overrides the Equals/GetHashCode() methods. In a separate test fixture, I want to assert that all the properties on Foo have been set properly, not just the properties used for equality. For this reason, my test assertion specifically uses the ShouldBeEquivalentTo method, which the documentation suggests will consider two objects to be equivalent if "both object graphs have equally named properties with the same value, irrespective of the type of those objects."

However, it appears that ShouldBeEquivalentTo invokes Foo.Equals method, gets a true result and then proceeds to short-circuit the reflection based property matching that ShouldBeEquivalentTo promises.

Is this expected behavior? If so, in inspecting the FA source, I saw no easy way to alter this behavior (IEquivalencyStep is declared internal). Are there any other ways to around this?

Edit: Dennis: Yes, the two objects I'm comparing are of the same type. To summarize, I have overridden Equals on class Foo, but do not want FA to use this notion of equality for my unit tests.

Mitch A
  • 2,050
  • 1
  • 21
  • 41
  • I don't entirely understand what you are trying to do. Is that object similar to the one you're invoking ShouldBeEquivalentTo() on? – Dennis Doomen Jan 10 '13 at 10:03
  • 2
    For future reference, Dennis addressed this issue here: http://fluentassertions.codeplex.com/discussions/428950, said that the behavior was changed in 2.1 – argaz Sep 19 '13 at 07:34
  • Argaz, if you turn your comment into an answer, I will accept it. – Mitch A Sep 22 '13 at 02:25

1 Answers1

0

I think you cannot alter behavior of this function, it assumes that if you override Equals - than you want comparison to be the same way. You can try the following function:

dto.ShouldHave().SharedProperties().EqualTo(foo);

Or you can implement in Foo class NativeEquals method which will be calling base.Equals() , and then use this method explicitly for comparison, it will work great for value types.

SergeyS
  • 3,515
  • 18
  • 27
  • 3
    Won't work, because SharedProperties() internally uses the same engine as ShouldBeEquivalentTo() does. I know, because I've designed it... – Dennis Doomen Jan 10 '13 at 10:10