Menu

C# – Unit testing a AutoMapper Profile mappings

When you use AutoMapper to map our objects, and AutoMapper profiles are the way we do it, it encapsulates a lot of business logic in some cases. But we need to make sure the business logic gets unit tested.

Below is how you can unit test the AutoMapper profiles, you will need to make sure you cover all the conditions

[TestMethod]
public void ShouldMapper_MapAdmitAllFields()
{
        //arrange
        var config = new MapperConfiguration(cfg => cfg.AddProfile<AdmitDtoProfile>());
        var mapper = config.CreateMapper();

        //act
        var result = mapper.Map<AdmitCommand, AdmitDto>(_command);

        //assert
        Assert.IsNotNull(result);
        Assert.AreEqual(result.Id, "1234567890");
        Assert.AreEqual(result.StartDate, new DateTime(1924, 10, 16));
        Assert.AreEqual(result.Code, "FULL");
}

Simple enough.

Leave a comment