Unit testing with Sitecore.FakeDb and deserialized data

Recently, I watched the excellent SVUG presentation by Pavel Veller on YouTube. I was impressed by the way Sitecore.FakeDb works. It’s much easier to configure than my FixtureDataProvider, has tighter isolation and is (because of that) much faster.

Even though the syntax for setting up your test data is very easy and short with Sitecore.FakeDb, I think that it can be a lot of work to setup things for a unit test. Besides, those of us using TDS or Unicorn are already putting a lot of information about the structure of items under source control. It would be a shame if we could not use that information.

That’s why I propose a hybrid approach: use DbItem and DbTemplate to setup your tests and where needed, deserialize individual items/templates. This can have the benefit of not having to initialize as much, as well as bringing your serialized data into the test scope.

On that last note: if a template field is renamed and then synchronized with TDS, then if the code you are unit testing relies on that field name… it will fail. Which can be very helpful during development.

I’ve added a project called Sitecore.FakeDb.Serialization to the FakeDb project. It’s usage is entirely optional and adds 2 important classes: DsDbItem and DsDbTemplate (Ds = Deserialized). They inherit from their regular Sitecore.FakeDb counterparts. But instead of having to define the field values, they load them from the filesystem.

You can easily mix these approaches. Here’s an example of a unit test using both:

 

You can find these changes in my fork on GitHub. To use them, you need to do the following afer you’ve setup Sitecore.FakeDb:
- Add a reference from your unit test project to Sitecore.FakeDb.Serialization.
- Edit the App.config file and add the following section (change the folders to point to your serialized data, or add/remove folders as needed):

 

Have fun coding!

Comments (0)

› No comments yet.

Leave a Reply

Allowed Tags - You may use these HTML tags and attributes in your comment.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Pingbacks (0)

› No pingbacks yet.

UA-4216957-1