1

I am doing integrative xunit tests on a .NET Core web application. The result of a call can sometimes be a large object with different levels of hierarchy.

     MainEntity
     {
        public decimal TotalTaxValue{ get; set; }

        public decimal GrossPrice{ get; set; }

        public List<Taxes> GroupedTotal { get; set; }

        //..... 20 more properties
      }

      Taxes 
      {

        public List<TaxCode> TaxCodes {get;set;}

        public decimal TaxedAmount{get;set;}
      }

    TaxCode 
    {
     public string TaxCodeType {get;set;} //VAT / EGAT

     public string ApplicableRegion {get;set;}
     }

Now when doing tests (using FluentAssertions) I end up with many lines of

item.PropertyName.Should().Be(output.PropertyName, because: "reason")

Also when going to compare the lists of objects inside the models, I found a few not nice to look at linq statements (which spread across multiple lines).

I should mention I don't have access to modify the original model and override the equals method.

Looking at how my code, with 10+ lines of assertions for a single test it can't be the best approach.

Another way I thought of approaching this would be to have a method to handle all of the assertions - but this only hides the issue. Also doing a test for each property, to avoid multiple assertions in a test, would be very time consuming, both in developing the tests and execution.

Question:

What are the best practices when comparing/(performing assertions on) larger objects in terms of assertions?

1

I found an solution for comparing larger objects (specific to my case, since it's actually part of FluentAssertions).

https://fluentassertions.com/objectgraphs/

From the documentation, it compares with a recursion of 10 levels and it it's used like this:

orderDto.Should().BeEquivalentTo(order);

I submit that a best practice to compare large objects is to use such a library if the objects don't have an equality comparer. This way you don't alter business code (by adding methods for testing) and not need multiple asserts in a test.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.