简体   繁体   中英

How can I mock data for unit tests?

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!

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
        }
    };
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM