为什么我的 NUnit 测试方法总是通过某些计算功能

问题描述 投票:0回答:1

如果我更改演示者方法的调用/实现,我的单元测试方法应该会失败。请看下面,如果不明白请告诉我。

    public SaveActionResultWithCustomizedErrors<int> SaveOrderAllocation(OrderAllocation orderAllocation, FillOrderOutput fillOrderOutput)
    {
        var saveResult = new SaveActionResultWithCustomizedErrors<int>();

        int? maxOfNinetyPercentAllocationQty = null;
        
        //// this is the class where I need to pass orderAllocation greater of either 90% of the Order Allocation Qty OR Pre Order Demand (Open + Released + Shipped).          
        var title = new orderAllocationInput() {Ean = orderAllocation.Ean}; 

        try
        {
            //validate pre order allocation qty before sending to product master using isUnlimited flag.
            if (!orderAllocation.IsUnlimitedAllocation)
            {
                if (orderAllocation.PreorderAllocationQty != null &
                    orderAllocation.PreorderAllocationQty.HasValue)
                {
                    var preDemandQty = (fillOrderOutput.OpenQuantity + fillOrderOutput.ReleasedQuantity + fillOrderOutput.ShippedQuantity);
                    maxOfNinetyPercentAllocationQty = Math.Max(.9 * orderAllocation.PreorderAllocationQty.Value, preDemandQty).RoundToInt();
                }

                if (maxOfNinetyPercentAllocationQty != null) title.PreorderAllocation = maxOfNinetyPercentAllocationQty.Value;
            }
           
            ItemDao.SavePreorderAllocation(orderAllocation, fillOrderOutput);
            saveResult.SuccessfulItems.Add(orderAllocation.SystemId, orderAllocation.SystemId);

        }
        this works fine, now I'm looking for a NUnit test method to fullfill the above calculation.
        
        
        
    [Test]
    public void SaveOrderAllocation_With_OrderAllocationToMaxOf90Percent_Or_PreDemandQtyTest()
    {
        // Arrange
                    
        var orderAllocation = new OrderAllocation
        {
            Ean = "EAN123",
            Sysid = 1,
            PreorderAllocationQty = 100,
            IsUnlimitedAllocation = false
        };

        var fillOrderOutput = new FillOrderOutput
        {
            OpenQuantity = 80,
            ReleasedQuantity = 30,
            ShippedQuantity = 20
        };

        var preDemandQty = fillOrderOutput.OpenQuantity + fillOrderOutput.ReleasedQuantity + fillOrderOutput.ShippedQuantity;
        // The preDemandQty is 80 + 30 + 20 = 130
        // 90% of 100 = 90, so max(90, 130) = 130

        //var expected = (int) (testParameter.PreorderAllocationQty * .9); 
        var expected  = Math.Max(.9 * orderAllocation.PreorderAllocationQty.Value, preDemandQty).RoundToInt();

        // Act
        var result = Testee.SaveOrderAllocation(orderAllocation, fillOrderOutput);

        // Assert
        _itemDaoMock.Verify(x => x.SavePreorderAllocation(orderAllocation, fillOrderOutput), Times.Once());

        Assert.IsTrue(result.Success);
        Assert.That(result.FailedItems.Count, Is.EqualTo(0));
        Assert.That(result.SuccessfulItems.Count, Is.EqualTo(1));
        Assert.AreEqual(expected, preDemandQty);  // 130
    }
    

现在,如果类中 SaveOrderAllocation 方法中的计算发生变化,我该如何断言。像下面这样

            if (!orderAllocation.IsUnlimitedAllocation)
            {
                if (orderAllocation.PreorderAllocationQty != null &
                    orderAllocation.PreorderAllocationQty.HasValue)
                maxOfNinetyPercentAllocationQty = (int?)(.9 * preorderAllocation.PreorderAllocationQty.Value);
                if (maxOfNinetyPercentAllocationQty != null) title.PreorderAllocation = maxOfNinetyPercentAllocationQty.Value;
            }

在这种情况下我上面的测试方法应该失败?如何在不更改方法 (SaveOrderAllocation) 主体的情况下断言。请帮忙。 预先感谢。

c# nunit
1个回答
0
投票

如果您将验证逻辑拆分为单独的公共方法(或者可能是单独的验证器,具体取决于您的用例),您可以单独测试该逻辑:

public int? MaxOfNinetyPercentAllocationQty(OrderAllocation orderAllocation, FillOrderOutput fillOrderOutput)
{
    if(orderAllocation.IsUnlimitedAllocation || orderAllocation.PreorderAllocationQty is null)
        return null;

    var preDemandQty = (fillOrderOutput.OpenQuantity + fillOrderOutput.ReleasedQuantity + fillOrderOutput.ShippedQuantity);
    maxOfNinetyPercentAllocationQty = Math.Max(.9 * orderAllocation.PreorderAllocationQty.Value, preDemandQty).RoundToInt();

    return maxOfNinetyPercentAllocationQty.Value;
}

在你的其他方法中,你只需这样调用它:

try
{
    var maxOfNinetyPercentAllocationQty = MaxOfNinetyPercentAllocationQty(orderAllocation, fillOrderOutput);
    if (maxOfNinetyPercentAllocationQty != null) title.PreorderAllocation = maxOfNinetyPercentAllocationQty.Value;
       
    ItemDao.SavePreorderAllocation(orderAllocation, fillOrderOutput);
    saveResult.SuccessfulItems.Add(orderAllocation.SystemId, orderAllocation.SystemId);
}

现在验证逻辑已分离出来,您可以为其编写测试并确保返回预期值。

© www.soinside.com 2019 - 2024. All rights reserved.