Olivier Muhring

Olivier Muhring

  • 1.5k
  • 262
  • 9.2k

FluentAssertions throws an excpetion while comparing 2 object models

Jun 6 2024 12:24 PM

In an API project, I'm consolidating data from separate external sources. One of these sources returns data in JSON format.

{
    "agency": "SomeAgency",
    "agencyData": {
        "brandColor": "#A70000",
        "cityName": "",
        "countryCode": "BE",
        "email": "",
        "iataCode": "12345678",
        "imageUrl": "",
        "name": "",
        "phoneContact": "",
        "postalCode": "",
        "stateProv": "",
        "street": ""
    },
    "agent": "[email protected]",
    "agentEmail": "[email protected]",
    "allowedPassengerUpdates": {
        "document_correction": {
            "allowed": true,
            "allowedPerPassengerType": {
                "ADT": true
            },
            "fieldsToUpdate": [
                "documentID",
                "documentType",
                "fiscalName",
                "citizenshipCountryCode",
                "residenceCountryCode",
                "issuingCountryCode",
                "expirationDate"
            ]
        },
...

I serialize this data into an object model, and use this model for the actual consolidation.

To validate this, I wrote the following unit test:

public void IsJsonValid()
{
    var expectedJson = LoadJson("Example.json");

    //  Generate a base object model using a generalized structure
    var expected = JToken.Parse(expectedJson);

    //  Generating an object model using the defined classes
    var actualModel = JsonConvert.DeserializeObject<OrderRetrieveResponse>(expectedJson);

    //  Converting the new object back into json
    var actualJson = JsonConvert.SerializeObject(actualModel);

    //  Generating a new object using a generalized structure
    var actual = JToken.Parse(actualJson);

    //  Comparing the 2
    actual.Should().BeEquivalentTo(expected);
}

This approach works fine if the data provider keeps its JSON clean and doesn't start mixing data types throughout the file.

When I execute the test, the comparison (actual.Should().BeEquivalentTo(expected)) throws an exception:

The exception more in detail: 

What should I do differently to avoid this issue?


Answers (3)