mardi 8 décembre 2020

How to avoid too many asserts or simplify testing mappings while unit testing AutoMapper's profiles using xUnit?

In my project I use one huge class with a lot of properties (legacy code) which is used in CosmosDb model. There is another database, the SQL one, which persist different structure of the class. So to sum up I have two classes (shortened examples):

public class CosmosDbModel
{
    [JsonProperty(PropertyName = "id")]
    public Guid Id { get; set; }

    [JsonProperty(PropertyName = "optionApproved")]
    public OptionApprovedCosmosDbEnum OptionApproved { get; set; }
}

public class SQLDbModel
{
    public Guid Id { get; set; }

    public OptionApprovedSqlDbEnum OptionAccepted { get; set; }
}

They differs with a lot of enums and other types like decimals againt integers and so on. I have created AutoMapperProfile which handle whole mapping issue. But now I have been struggling with unit testing it.

Is there any chance to avoid that kind of test?

[Fact]
    public async Task TestAutoMapper()
    {
        // Arrange
        var dto = new CosmosDbModel
        {
            Id = "someId",
            OptionApprovedCosmosDbEnum = OptionApprovedCosmosDbEnum.Approved
        };

        // Act
        var result = Mapper.Map<SQLDbModel>(dto);

        // Assert
        Assert.Equal(OptionApprovedSqlDbEnum.Accepted, result.OptionAccepted);
        .
        .
        ... and many many assertions...
    }

I mean - I would like to get rid of wrtitting like dozens of assertions and check it somehow automatically. Is it possible? I know that there exist at least AssertConfigurationIsValid method from AutoMapper which helps check if all fields/properties are used in the profile mapping of mapped classes. But is there any easy way to write assertion code or at least be warned if some of the fields haven't been yet contained in assert section?

I use 8.1 version of AutoMapper.

Aucun commentaire:

Enregistrer un commentaire