Home > Sitecore 6, Unit Testing > Unit testing Sitecore with Typemock Isolater.

Unit testing Sitecore with Typemock Isolater.

In my last post I gave a stub example for a Sitecore Item. With the stub item you could test functionality for retrieving field data, which greatly limits the functionality you can test. So in this post I will have a look at the more advanced mocking framework Isolator from Typemock.  With the Isolator framework it is possible to intercept calls for methods that normally wouldn’t be possible, for example static methods. How may this help us? Since Sitecore hasn’t made it any easier to do unit testing with a great deal of static classes and methods alike, you will be able to mock or stub them out with the Isolator framework. The following is a simple example for testing functionality for Item.Children, ie. Sitecores Childlist.

Here we have a simple piece of code that, given a specific Template ID, adds an item to a list and returns the list.

public class SectionFactory
{
   public static IEnumerable<Section> CreateCollection(Item columnItem)
   {
      ID sectionTemplateId = new ID(Constants.Templates.Section);
      ChildList children = columnItem.Children;
      List<Item> sections = new List<Section>();
      foreach (Item child in children)
      {
         if (child.TemplateID == sectionTemplateId)
            sections.Add(new Section(child))

      }

     return sections;
 }
}

Since getting the children for a Item invokes more than a few static methods and classes it is impossible to stub the functionality using most mocking frameworks. But with the Isolator Framework it is possible.

[TestFixture]
 public class SectionFactoryTest
 {
 [Test]
 public void SectionFactoryNoChildrenReturnEmpyList()
 {
 Item stub = Isolate.Fake.Instance<Item>();
 ChildList childlist = Isolate.Fake.Instance<ChildList>();
 Isolate.WhenCalled(() => stub.Children).WillReturn(childlist);
 IEnumerable<Section> sectionList = SectionFactory.CreateCollection(stub);
 Assert.AreEqual(new List<Section>(), sectionList);
 }

[Test]
 public void SectionFactoryOneChildReturnEmpyList()
 {
 Item stub = Isolate.Fake.Instance<Item>();
 Item child = Isolate.Fake.Instance<Item>();
 ChildList childlist = Isolate.Fake.Instance<ChildList>();

ID sectionTemplate = new ID(Constants.Templates.Section);
 Isolate.WhenCalled(() => child.TemplateID).WillReturn(sectionTemplate);
 Isolate.WhenCalled(() => childlist).WillReturnCollectionValuesOf(new List<Item>(){child});
 Isolate.WhenCalled(() => stub.Children).WillReturn(childlist);

ID link = new ID(Constants.Fields.Section.Links);
 Isolate.WhenCalled(() => LinkRepository.GetCollection(child, link)).WillReturn(null);

IEnumerable<Section> sectionList = SectionFactory.CreateCollection(stub);
 Section section = sectionList.FirstOrDefault();
 Assert.That(section, Is.Not.Null);

 }

[Test]
 public void SectionFactoryOneValidSectionChildChildReturnEmpyList()
 {
 Item stub = Isolate.Fake.Instance<Item>();
 Item sectionChild = Isolate.Fake.Instance<Item>();
 Item child = Isolate.Fake.Instance<Item>();
 ChildList childlist = Isolate.Fake.Instance<ChildList>();

ID sectionTemplate = new ID(Constants.Templates.Section);
 Isolate.WhenCalled(() => sectionChild.TemplateID).WillReturn(sectionTemplate);
 Isolate.WhenCalled(() => child.TemplateID).WillReturn(null);
 Isolate.WhenCalled(() => childlist).WillReturnCollectionValuesOf(new List<Item>() { child, sectionChild });
 Isolate.WhenCalled(() => stub.Children).WillReturn(childlist);

ID link = new ID(Constants.Fields.Section.Links);
 Isolate.WhenCalled(() => LinkRepository.GetCollection(child, link)).WillReturn(null);

IEnumerable<Section> sectionList = SectionFactory.CreateCollection(stub);

 Assert.AreEqual(1,sectionList.Count());

}
 }

So with the above test we managed to get a 100% test coverage for the code.
And with the Isolator Framework we could even mock out the Sitecore.Context. The Isolater framework is must a have if you want to do unit testing with Sitecore.

Advertisements
  1. 27/04/2012 at 09:38

    Great post – this looks very clean. Does Typemock have the same sort of build overhead as Moles?

    • 27/04/2012 at 09:56

      I Haven’t tried Moles, but the build overhead if any is almost none existing.

  1. 29/04/2012 at 19:12
  2. 07/06/2013 at 06:53
  3. 07/06/2013 at 11:58

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: