Linq Sum issue when NCrunch Testing

54 Views Asked by At

When running a unit test with NCrunch I am having difficulty resolving certain behaviour.

If I have a line of code such as

itemWeight += ChildItems.Sum(ci => ci.ItemWeightManufacturedInTonnes);

versus

ChildItems.ForEach(ci => itemWeight += ci.ItemWeightManufacturedInTonnes);

When running with NCrunch and MBUnit the test is failing with

System.ArgumentException: Delegate to an instance method cannot have null 'this'.

when using the first line of code.

By changing to the second line of code, where ForEach is a handcrafted method to apply function to each item in a collection, then the tests work as expected.

The underlying collection is an ObjectSet from EntityFramework if that makes a difference.

The ChildItems are mocked items using TypeMock. Perhaps Linq functions require something that is not being configured correctly.

Could someone explain why there would be a difference in behaviour please.

UPDATE:

Foreach function:

    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
0

There are 0 best solutions below