使用NCrunch运行单元测试时,我很难解决某些行为。
如果我有一行代码,例如
itemWeight += ChildItems.Sum(ci => ci.ItemWeightManufacturedInTonnes);
与
ChildItems.ForEach(ci => itemWeight += ci.ItemWeightManufacturedInTonnes);
使用NCrunch和MBUnit运行时,测试失败
System.ArgumentException:委托给实例方法不能 null' this'。
使用第一行代码时。
通过更改为第二行代码,ForEach是一个手工制作的方法,可以将函数应用于集合中的每个项目,然后测试按预期工作。
底层集合是来自EntityFramework的ObjectSet,如果这会产生影响。
ChildItems是使用TypeMock的模拟项目。也许Linq函数需要一些没有正确配置的东西。
有人可以解释为什么行为会有所不同。
更新:
Foreach功能:
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
if (source == null) throw new ArgumentNullException("source");
if (action == null) throw new ArgumentNullException("action");
foreach (T item in source)
{
action(item);
}
}
ChildItems
public EntityCollection<Item> ChildItems