Wednesday, October 28, 2015

NSubstitute

One of the key benefits of unit testing is that it pushes you to code to abstractions. Each class has a clear inside and outside, and your tests document the inside bits. The outside stays wrapped in an interface or abstract class so we can change its behavior in tests. When you go down this path, having a tool to make fake versions of interfaces in a simple and painless way is key, which is why I recommend NSubstitute. It is elegantly simple, very flexible, and makes it hard to get into trouble. In this post I will go over some fundamentals of NSubstitute, and compare them to how things work in the more well known Moq library.

Wednesday, October 21, 2015

Unit Testing an Event Handler with Sitecore.FakeDB

In my last post, we looked at how to make an event handler unit testable by wrapping an interface around the Sitecore log, so that we could use NSubstitute to verify the calls made to it.

This only part of the story, of course. For our tests to be meaningful, we need to be able to "arrange" the input that goes into our code, before we make it "act" and before we "assert" how it should behave. Since we are building an ItemSaved handler, we need a way to create Sitecore items in our unit tests.  There is a tool, Sitecore.FakeDB, that makes this straigtforward.

Wednesday, October 14, 2015

Testable Event Handlers using Sitecore's Configuration Factory

It's a not widely know feature of Sitecore configuration that it allows you to specify parameters for event handlers and pipeline processors. This can be used in combination with wrapper interfaces to write unit testable Sitecore code.