How can I pass dynamic objects into an NUnit TestCase function?

后端 未结 4 685
忘了有多久
忘了有多久 2020-12-12 22:10

I am writing a data-intensive application. I have the following tests. They work, but they\'re pretty redundant.

[Test]
public void DoSanityCheck_WithCountEqu         


        
4条回答
  •  情话喂你
    2020-12-12 22:11

    It would be much much easier to have a private method, base class method, or helper classes that do this for you.

    For my unit tests, I need many many mock entities because it's a very data-intensive application. I've created a structure of mock repositories that can create initialized entities on the fly, which I can combine to build up a representative database structure in memory.

    Something like that could work for you:

    // Wild guess at the class name, but you get the idea
    private void InitializeTotals(AggregateItem item)
    {
        item.ItemCount = 0;
        item._volume = 0;
        item._houseGross = 1;
    }
    
    [Test]
    public void DoSanityCheck_WithCountEqualsZeroAndHouseGrossIsGreater_InMerchantAggregateTotals_SetsWarning()
    {
        InitializeTotals(report.Merchants[5461324658456716].AggregateTotals);
    
        report.DoSanityCheck();
    
        Assert.IsTrue(report.FishyFlag);
        Assert.That(report.DataWarnings.Where(x => x is Reports.WarningObjects.ImbalancedVariables && x.mid == 5461324658456716 && x.lineitem == "AggregateTotals").Count() > 0);
    }
    
    [Test]
    public void DoSanityCheck_WithCountEqualsZeroAndHouseGrossIsGreater_InAggregateTotals_SetsWarning()
    {
        InitializeTotals(report.AggregateTotals);
    
        report.DoSanityCheck();
    
        Assert.IsTrue(report.FishyFlag);
        Assert.That(report.DataWarnings.Where(x => x is Reports.WarningObjects.ImbalancedVariables && x.mid == null && x.lineitem == "AggregateTotals").Count() > 0);
    }
    
    [Test]
    public void DoSanityCheck_WithCountEqualsZeroAndHouseGrossIsGreater_InAggregateTotalsLineItem_SetsWarning()
    {
        InitializeTotals(report.AggregateTotals.LineItem["WirelessPerItem"]);
    
        report.DoSanityCheck();
    
        Assert.IsTrue(report.FishyFlag);
        Assert.That(report.DataWarnings.Where(x => x is Reports.WarningObjects.ImbalancedVariables && x.mid == null && x.lineitem == "WirelessPerItem").Count() > 0);
    }
    

提交回复
热议问题