Unit Testing Collections

loopI have written about Lowering the friction of Unit testing, and how using Aspect Faking we can lower the friction of testing collections

The problem

Unit testing collections can be a bit tricky. Lets see some code and unit test this Sum() method:

public List<Salary> Salaries { get; }

public float Sum() { float total = 0; foreach (var item in Salaries) { total += item.Value; } return total; }

class Salary { public float Value { get; set; } ... }

Testing this can be simple: just supply a collection with fake items and run the code.

[TestMethod] public void Sum_Adds3Items() { var fakeSalaries = new List<Salary> {

new Salary { Value = 5000 },

new Salary { Value = 6000 },

new Salary { Value = 5500 } };

var payments = new Payments();

payments.Salaries = fakeSalaries; Assert.AreEqual(16500, payments.Sum()); }

Simple, but in some cases, it is impossible to create the fake list. Examples are: Linq queries, SharePoints SPListCollection and SPItemCollection and DevExpress XPCollection. This are all custom collections and cannot be instantiated. We want to fake these collections, but we want to do it in a resilient way. Resilient enough that if we can choose to implementation via direct calls on the index without failing our tests.

public XpCollection<Salary> Salaries { get; set; }


float total = 0;

for (int i = 0; i<Salaries.Count; i++) { total += Salaries[i].Value; } return total;

Here is where Faking the Collection Aspect comes into play, we want to fake just the Collection-Aspect the XpCollection and swap it with a collection of our choice.

   new[] {
        new Salary { Value = 5000 }, 
        new Salary { Value = 6000 }, 
        new Salary { Value = 5500 } 
Note how we are just passing an array!.

Now we can test our code and it won’t break if we change the implementation from using the enumerator of using direct access through the indexer.

This feature is soo useful that it got its own API:
Isolate.WhenCalled(() => payment.Salaries)
   .WillReturnCollectionValuesOf( new[] 
        new Salary { Value = 5000 }, 
        new Salary { Value = 6000 }, 
        new Salary { Value = 5500 } 

Once again, we manage to lower the friction of unit testing and create resilient tests.

Add Comment

Required fields are marked *. Your email address will not be published.