xUnit .NET Core多个InlineData输入分别具有不同的期望

时间:2019-04-24 14:01:51

标签: c# .net-core xunit

        [Theory]
        [InlineData("atv,atv,atv,hdm")]
        [InlineData("nx9,nx9,atv,nx9,nx9,nx9")]
        [InlineData("nx9,nx9,atv,nx9,nx9,nx9,nx9")]
        public void Buy3Pay2(string input)
        {           
            var co = new Checkout(rules);

            var stock = input.Split(',');

            foreach (var item in stock)
            {
                var product = products.SingleOrDefault(p => string.Compare(item, p.SKU, true) == 0);
                Item realItem = new Item() {
                    Name = product.Name,
                    SKU = product.SKU,
                    Price = product.Price
                };
                realItem.ID = Guid.NewGuid().ToString();
                co.Scan(realItem);
            }

            var total = co.Total();

            total.Should().Be(249.00M);
            //total.Should().Be(531.00M); for the second input
            //total.Should().Be(698.00M); for the 3rd input
        }

如何根据不同的InlineData输入设置预期结果?

1 个答案:

答案 0 :(得分:2)

也将其(期望)添加为输入,并将其用于断言

例如

[Theory]
[InlineData("atv,atv,atv,hdm", "249.00")]
[InlineData("nx9,nx9,atv,nx9,nx9,nx9", "531.00")]
[InlineData("nx9,nx9,atv,nx9,nx9,nx9,nx9", "698.00")]
public void Buy3Pay2(string input, string expected) {
    //Arrange
    var co = new Checkout(rules);

    var stock = input.Split(',');

    foreach (var item in stock) {
        var product = products.SingleOrDefault(p => string.Compare(item, p.SKU, true) == 0);
        Item realItem = new Item() {
            Name = product.Name,
            SKU = product.SKU,
            Price = product.Price
        };
        realItem.ID = Guid.NewGuid().ToString();
        co.Scan(realItem);
    }

    //Act
    var total = co.Total();

    //Assert
    total.Should().Be(Convert.ToDecimal(expected));            
}