Unit Testing Collections
I 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; }
public 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.
Isolate.Swap.CallsOn(payment.Salaries).WithCallsTo( new[] { new Salary { Value = 5000 }, new Salary { Value = 6000 }, new Salary { Value = 5500 } });
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.
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.
Recent Posts
- Top 5 questions to ask when checking references
- Unacceptable: Unit testing will take 20 years to catch on
- The 4 reasons why we DIDN’T choose Oslo
- Typemock Academy Launch
- The First Rule to Software Craftsmanship
Categories
- .NET Tests
- Agile
- Code Integrity
- Community
- Debugging
- Fun
- Management for Geeks
- Marketing
- Product
- Release
- Reviews
- SharePoint
- TDD
- Time Management
- Uncategorized
- Unit Tests
Archives
- August 2010
- May 2010
- April 2010
- March 2010
- February 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- December 2008
- November 2008
- August 2008
- July 2008
- May 2008
- April 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
