sergeyshushlyapin edited this page May 26, 2017 · 10 revisions

Sitecore FakeDb is the unit testing framework for Sitecore that enables creation and manipulation of Sitecore content in memory. It enables unit testing for areas that are not unit testable from the first look (e.g. static classes) or require significant efforts to create and maintain (e.g. a content tree). It is designed to minimize efforts for the test content initialization keeping focus on the minimal test data rather than comprehensive content tree representation.

Here is a typical FakeDb unit test:

public void HowToCreateSimpleItem()
  using (var db = new Db
      new DbItem("Home") { { "Title", "Welcome!" } }
    Sitecore.Data.Items.Item home = db.GetItem("/sitecore/content/home");
    Xunit.Assert.Equal("Welcome!", home["Title"]);

Sitecore FakeDb perfectly interacts with NCrunch running hundreds of lightweight unit tests in seconds. It does not set a goal to run the entire Sitecore instance in-memory or to be used for integration testing.

The term unit test is often misunderstood in Sitecore world. There are numerous articles that describe how to configure "unit tests" but those tests require database connection or even Web runner which makes them integration tests. There is nothing bad in this kind of tests, they are just for another purpose. Sitecore FakeDb is aimed for writing tests which could correspond to the definition given by Roy Osherove in "The Art Of Unit Testing":

A good unit test is:

  • Able to be fully automated
  • Has full control over all the pieces running (Use mocks or stubs to achieve this isolation when needed)
  • Can be run in any order if part of many other tests
  • Runs in memory (no DB or File access, for example)
  • Consistently returns the same result (You always run the same test, so no random numbers, for example. save those for integration or range tests)
  • Runs fast
  • Tests a single logical concept in the system
  • Readable
  • Maintainable
  • Trustworthy (when you see its result, you don’t need to debug the code just to be sure)

FakeDb substitutes Sitecore providers with fake "in-memory" providers to avoid calls to database or configuration. The providers do not replicate real Sitecore behavior, but are used mainly as stubs with minimal logic. They can be replaced with mocks in unit tests using the provider switchers.

Getting started

For contributors

In order to get started you'll need to perform the following steps:

  1. Fork and clone the repository
  2. Put the license.xml file into the lib folder
  3. Run build.bat and wait until the build process downloads all the required NuGet dependencies including Sitecore libraries.

Please note that Sitecore FakeDb supports wide range of Sitecore versions. The build process compiles FakeDb and run unit tests against each of the supported versions. This may take a while.

When the build is done, the lib folder contains Sitecore 8.1 rev. 160519 assemblies (Update-3). You're ready to go!

For framework users

Proceed to the Installation page.