0

I have a method which tests a unit that changes state of the layout object that is a POCO:

    [TestMethod]
    public void A_Test_Method()
    {
        // Arrange
        const double adjustmentValue = 50;

        var expectedLayout = CreateDefaultLayout();
        ... // set some values

        var actualLayout = CreateDefaultLayout();
        ...

        var runtimeHelper = new LayoutRuntimeHelper(actualLayout);

        // Act

        runtimeHelper.SetPrintVerticalAdjustment(actualLayout.Print.VerticalAdjustment.Orientation, adjustmentValue);

        // Assert

        Assert.AreEqual(expectedLayout, actualLayout);
    }

Note: the ToString() method is overridden so it prints entire object like JSON, not just its type. Same happens with Equals().

The problem is that layout objects are simply big, so the test output becomes huge as well where I have to find the differences between those objects. So, how to tell the testing framework to output only the differences?

4
  • You could create a method that returns an IEnumerable of differences. If Count is 0 it’s equal otherwise you could concat the differences into the Exception-Message. Commented Apr 11, 2019 at 22:54
  • 3
    FluentAssertions can generally do this and integrates with all the major test frameworks. Use Should().BeEquivalentTo. You might have to modify the ToString implementation or tell the library how to print objects because normally they are ToString'd. Commented Apr 11, 2019 at 23:51
  • @MikeZboray Thanks for the suggestion, works like a charm. Also, removed ToString() overridings and it still outputs the good message. Commented Apr 12, 2019 at 14:06
  • @MikeZboray looks like Sould().BeEquivalentTo() loops through properties, retrieves different ones and logs them. That can be achieved with System.Reflection and not using ToString(). Also, having decompiled the .dll I've seen it does have System.Reflection in its depencencies. Commented Apr 12, 2019 at 14:36

1 Answer 1

1

Thanks to Mike Zboray's comment, FluentAssertions Nuget package makes it perfect. Had to replace this:

Assert.AreEqual(expectedLayout, actualLayout);

to this:

actualLayout.Should().BeEquivalentTo(expectedLayout);

Test output:

Expected member Image.VerticalPositionAbsoluteMm to be 550.0, but found 650.0.
Expected member Print.VerticalPositionAbsoluteMm to be 550.0, but found 650.0.
Expected member Print.VerticalAdjustment.Orientation to be -1M, but found 1M.

With configuration:
- Use declared types and members
- Compare enums by value
- Match member by name (or throw)
- Without automatic conversion.
- Be strict about the order of items in byte arrays

... (Exception Stacktrace)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.