Should, ShouldBe; a silly mistake using FluentAssertions

I use FluentAssertions for all of my asserting needs. I like it’s API better than the assert methods you get with the .Net framework, and the FluentAssertions library provides an overall more fully featured set of assertion options. It’s open-source and continually updated, too, which makes it all right in my book. Sometimes, though, while I’m furiously writing tests, I get this test failure signature and it catches me off guard.

Subject has property Subject that the other object does not have.

Here’s a sample NUnit test case that would cause this particular error.

internal class MistakesTests
{
    [Test]
    public void ThisDumbThingIKeepDoing()
    {
        var thing = new Thing { FirstName = "Nelson" };
        var thing2 = new Thing { FirstName = "Nelson" };

        // This should definitely pass, right?
        thing.Should().ShouldBeEquivalentTo(thing2);
    }

    private class Thing
    {
        public string FirstName { get; set; }
    }
}

As you can see, I’m just doing a very simple object graph comparison, but it isn’t working as I intended. Can you spot the error?

Whereas I should be calling ShouldBeEquivalentTo on my Thing instance, I’m actually calling it on an instance of ObjectAssertions type from the FluentAssertions library. Whoops! 😳

Leave a comment ?

12 Comments.

  1. Life saver!

  2. Tisk tisk. Wher is that wall of shame?

  3. Thank you,
    I had the same problem, you saved me 😀

  4. I was scratching my head over this, as I had something like

    expected.Should().NotBeNull().And.ShouldBeEquivalentTo(actual);

    Thank you!

  5. Good man!!! Saved me as well – what a terrible error message.

  6. I feel so dumb right now. Thanks 😀

  7. Thanks – just happened to me 🙂

  8. Silly mistake but apparently a lot of people have made it! Thanks for pointing me in the right direction.

  9. +1 Thank you 🙂

  10. What everyone else has said – it certainly had me scratching my head for 5 minutes.

  11. +1 your, -1 for this silly error message

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>