Home > Back-end >  How can I mock data for unit tests?
How can I mock data for unit tests?

Time:12-07

I am trying to create an elegant way to write dummy data for my tests without copy pasting.

public void AssetIndexPortfolioCompositionMappingTest()
{
    var AssetIndexDTO = new AssetIndexSummaryDto()
    {
        PortfolioComposition = new PortfolioCompositionDto()
        {
            FaceValue = decimal.Zero,
            InsolvencyCasesPercentage = 1,
            LegalCasesPercentage = 2,
            NumberOfAccounts = 3,
            NumberOfCustomers = 4,
            NumberOfTotalPayments = 5,
            Principal = 6.7m
        }
    };
            
    var AssetIndexEntity = new AssetIndexEntity();

    _mapper.Map(AssetIndexDTO, AssetIndexEntity);
    // Assert

    AssetIndexDTO.PortfolioComposition.FaceValue.Should().Be(AssetIndexEntity.FaceValue);
    AssetIndexDTO.PortfolioComposition.LegalCasesPercentage.Should().Be(AssetIndexEntity.LegalCasesPercentage);
    AssetIndexDTO.PortfolioComposition.NumberOfAccounts.Should().Be(AssetIndexEntity.NumberOfAccounts);
    AssetIndexDTO.PortfolioComposition.NumberOfCustomers.Should().Be(AssetIndexEntity.NumberOfCustomers);
    AssetIndexDTO.PortfolioComposition.NumberOfTotalPayments.Should().Be(AssetIndexEntity.NumberOfTotalPayments);
    AssetIndexDTO.PortfolioComposition.Principal.Should().Be(AssetIndexEntity.Principal);
}

Here is my test and the part where I initialize a new object of AssetIndexDTO, I want to mock it so I can re use it in all my tests without copy pasting it every time

What I tried is using moq

var mock = new Mock<AssetIndexSummaryDto>();
mock.SetupAllProperties();

and then in assert I am trying to compare it to the mapped value

mock.Object.PortfolioComposition.FaceValue.Should().Be(AssetIndexEntity.FaceValue);

but it isnt working and throwing an error of System.NullReferenceException: 'Object reference not set to an instance of an object.'

Thanks in advance!

CodePudding user response:

You don't generally mock data, you mock functionality that is not relevant/necessary for tests.

Just move the code that generates the dummy data to a method and call in all of your tests:

public void AssetIndexPortfolioCompositionMappingTest()
{
    var AssetIndexDTO = GetTestAssetIndexSummaryDto();
    var AssetIndexEntity = new AssetIndexEntity();
    _mapper.Map(AssetIndexDTO, AssetIndexEntity);
    
    // Assert
    AssetIndexDTO.PortfolioComposition.FaceValue.Should().Be(AssetIndexEntity.FaceValue);
    AssetIndexDTO.PortfolioComposition.LegalCasesPercentage.Should().Be(AssetIndexEntity.LegalCasesPercentage);
    AssetIndexDTO.PortfolioComposition.NumberOfAccounts.Should().Be(AssetIndexEntity.NumberOfAccounts);
    AssetIndexDTO.PortfolioComposition.NumberOfCustomers.Should().Be(AssetIndexEntity.NumberOfCustomers);
    AssetIndexDTO.PortfolioComposition.NumberOfTotalPayments.Should().Be(AssetIndexEntity.NumberOfTotalPayments);
    AssetIndexDTO.PortfolioComposition.Principal.Should().Be(AssetIndexEntity.Principal);
}

private AssetIndexSummaryDto GetTestAssetIndexSummaryDto()
{
    return new AssetIndexSummaryDto()
    {
        PortfolioComposition = new PortfolioCompositionDto()
        {
            FaceValue = decimal.Zero,
            InsolvencyCasesPercentage = 1,
            LegalCasesPercentage = 2,
            NumberOfAccounts = 3,
            NumberOfCustomers = 4,
            NumberOfTotalPayments = 5,
            Principal = 6.7m
        }
    };
}

CodePudding user response:

If you want to avoid copy-pasting, I suggest you to implement some method that creates that object. And reuse that method in your tests. I do not see the need of mocking here.

  • Related