0
我目前正在接受有关unittests和LINQ的教育,但我需要一些帮助。单元测试LINQ枚举
什么是单元测试像这些方法的正确方法:
/// <summary>
/// Returns the product of all numbers.
/// </summary>
static public decimal Product<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector)
{
return source.Aggregate<TSource, decimal>(1, (current, s) => current * selector(s));
}
/// <summary>
/// Returns the product of all numbers.
/// </summary>
static public decimal? Product<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
{
return source.Select(selector).Aggregate<decimal?, decimal?>(1, (current, i) => current * i ?? 1);
}
我真的无法弄清楚如何。无论我一直在努力,当运行代码覆盖率时,几个块始终保持未被覆盖。
我曾尝试:
[TestMethod]
public void ExtendedProductAOfDecimalTest()
{
List<decimal?> items = new List<decimal?>(new decimal?[] { 5, 10, 15, 20, 25, 30 });
Assert.AreEqual(11250000, Enumerable.Product(items, i => i));
}
[TestMethod]
public void ExtendedProductBOfDecimalTest()
{
List<decimal> items = new List<decimal>(new decimal[] { 5, 10, 15, 20, 25, 30 });
Assert.AreEqual(11250000, Enumerable.Product(items, i => i));
}
运行此时,预期的答案更改为0.是否预期的行为?我会认为它不是。编辑:当然,我应该买新的眼镜。 – Matthijs
在重写了一些代码后,它完美地工作。我使用了旧的选择器而不是这里显示的选择器。 – Matthijs