Tag Archives: Sitecore

Unit Testing with Sitecore and Fortis – Part I

This post is the start of a series of articles looking at unit testing using Sitecore and Fortis. There are already a lot of great posts on unit testing with Sitecore using fakes and other tools, so we are going to primarily focus on how to setup unit testing when using the Fortis framework. (I will go into some of the other benefits of using Fortis in a future post).

One of the really useful things about Fortis is how it abstracts away the Sitecore API and models your data templates. All this is done via the IItemFactory and IItemWrapper intefaces, allowing you to easily unit test your code logic without having to worry about mocking/faking Sitecore.

In this post we will discuss how to setup the unit tests and give some simple examples. In following posts we will go more in depth and cover unit tests for more complex code.


Here at Lightmaker we group our components logically by function rather than by type. Each component consists of one or more of the following:

  • Rendering: A view or controller rendering that holds the presentation elements
  • Controller: A simple controller action that requests the data from a manager class and sends the model to the view/rendering
  • Manager: The object that does the heavy lifting and logic. This is the main part of our code that gets unit tested
  • Rendering Model: The model for the view. This always inherits from Fortis.Models.IRenderingModel so that all views have access to the PageItem, the RenderingItem and the RenderingParametersItem*


So let’s look at a code example. Let’s take a component that display’s frequently asked questions. For this we have the following items created in Sitecore:

  • Templates:
    • Frequently Asked Question: This template contains 2 fields, Question (single-line text) and Answer (Rich Text)
    • Frequently Asked Questions Folder: Inherits from the system/common/folder template, has insert options set to Frequently Asked Question.
    • Content Page: A generic page type that contains the scaffolding for site pages.
  • Renderings:
    • Frequently Asked Question: A simple controller rendering
  • Managers:
    • IFAQManager : The main manager class to handle the business logic

Project Setup

Here is the basic project setup:
Project Setup

The Code

The IFAQManager contains a single method – GetFAQsFromParent – that takes an IFrequentlyAskedQuestionsFolder object and return an IEnumerable.

The interface and implementation:

public interface IFAQManager
	IEnumerable GetFAQsFromParent(IFrequentlyAskedQuestionsFolder parentFolder);
public class FAQManager : IFAQManager
	public IEnumerable GetFAQsFromParent(IFrequentlyAskedQuestionsFolder parentFolder)
		var childItems = parentFolder.Children().OrderBy(x => x.Name);
		return childItems;

The controller:

public class FAQController : BaseController
	protected readonly IFAQManager FAQManager;

	public FAQController(ICustomItemFactory itemFactory, ILoggingManager log, IFAQManager faqManager)
		: base(itemFactory, log)
		this.FAQManager = faqManager;

        public ActionResult FAQList()
	        var model =
		        new FAQRenderingModel(
				        .GetRenderingContextItems<IContentPage, IFrequentlyAskedQuestionsFolder, IFAQRenderingParameters>(

			if (model.RenderingItem == null)
				throw new RenderingParametersException("Data source");

			model.FrequentlyAskedQuestions = this.FAQManager.GetFAQsFromParent(model.RenderingItem);
			return this.View(model);

The model:

public class FAQRenderingModel : RenderingModel<IContentPage, IFrequentlyAskedQuestionsFolder, IFAQRenderingParameters>
	public FAQRenderingModel(IRenderingModel<IContentPage, IFrequentlyAskedQuestionsFolder, IFAQRenderingParameters> model)
		: base(model.PageItem, model.RenderingItem, model.RenderingParametersItem, model.Factory)

	public IEnumerable<IFrequentlyAskedQuestion> FrequentlyAskedQuestions { get; set; }

The Unit Test

We will be using Moq and NUnit to complete the testing. Here is the code for the Unit Test:

public class FAQManagerTests
	public void GetFAQsFromParentTest()
		// Create a mock of the datasource item
		var dataSource = new Mock<IFrequentlyAskedQuestionsFolder>();

		// Create some test data
		var faq1 = new Mock<IFrequentlyAskedQuestion>();
		var faq2 = new Mock<IFrequentlyAskedQuestion>();
		var faq3 = new Mock<IFrequentlyAskedQuestion>();
		var faq4 = new Mock<IFrequentlyAskedQuestion>();

		var faqList = new List<IFrequentlyAskedQuestion> { faq1.Object, faq2.Object, faq3.Object, faq4.Object };

		// Setup the datasource to return the child items.
		dataSource.Setup(x => x.Children<IFrequentlyAskedQuestion>(recursive: false)).Returns(faqList);

		// Create the FAQ Manager
		var faqManager = new FAQManager();

		var returnedItems = faqManager.GetFAQsFromParent(dataSource);
		Assert.AreEqual(faqList.Count, returnedItems.Count());

This is a very basic example of how to unit test with Fortis. In the next posts in this series, we will breakdown whats happening here and look at some more complex test cases, including testing the controllers.

* The Fortis rendering model has the following properties:

  • PageItem: A model of the current Sitecore context item. Equivalent of Sitecore.Context.Item
  • RenderingItem: A model of the item set by the renderings DataSource
  • RenderingParametersItem: A model of the rendering parameters associated with the current rendering

Sitecore Technical Articles

Lightmaker are excited to announce a new series of technical articles based around our experiences with delivering Sitecore implementations, written by our Global Sitecore Practice Lead – Richard Seal. These articles will cover many technical subjects including, Best Practices, Sitecore Custom Development, Handy Developer Tips, New Features and more.

As a Sitecore Partner, Lightmaker has delivered a number of high quality Sitecore implementations. Last year our implementation for the Barclays ATP World Tour Finals won Sitecore Site of the Year Award for the Best Entertainment, Sports and Leisure Site category.

We will also bring you the latest news on new Sitecore releases and what the new features mean in the real world, and on the development of Fortis, a joint open source project between Lightmaker and Sitecore MVP Jason Bert

With the release of Sitecore 8 rapidly approaching and the improvements in the Experience Database, it is an exciting time to be a Sitecore developer!

–  Richard Seal

ATP Wins Sitecore’s Sports & Entertainment Site of the Year

The Lightmaker team is thrilled to announce that Sitecore, a leading web content management and customer experience management software company, announced today that the Barclays ATP World Tour Finals has been named the North American Sitecore Sports, Entertainment and Leisure Site of the Year winner.

The ATP is the governing body of the men’s professional tennis circuits – the ATP World Tour, the ATP Challenger Tour and the ATP Champions Tour. At the end of the season only the World’s Top 8 qualified singles players and doubles teams, based on their performance throughout the year, will qualify to compete for the last title of the season at the Barclays ATP World Tour Finals.

Lightmaker, aligned with ATP as their creative and technology partner, provided strategic planning and development for the digital roadmap initiatives. The Barclays ATP World Tour Finals was the first major initiative and website planning began in early 2013 with an extensive discovery process and culminated in the launch of a ground-breaking digital experience for this historic event.

The new website, designed and built in collaboration with ATP, features enhanced data visualizations, player bios, searing imagery and an integrated presentation of news, video, photos and social media. The site challenges conventional sporting event websites with its bold, clean design. The site’s responsive design provides a consistent yet intuitive presentation to millions of fans around the world regardless of the devices they are using.

Sitecore has been a large part of our growing sports and entertainment business, and continues to be a great partner for Lightmaker. We look forward to building more great sites on the Sitecore platform.

Complete list of Sitecore Site of the Year winners: VISIT AWARD SITE