Sitecore + TDD: let’s start

I love TDD and think it should be used by all developers. Sitecore code can be covered by tests and the great tool for that purpose is Sitecore.FakeDb created by Sergey Shushlyapin.

In the current blog post, I’ll show how to test simple scenario. The context is – let’s imagine that we develop custom command and the button which runs this command should be enabled in case if some folder (in the code below folder id is ‘{B4F0DAD0-B60E-49D9-8332-8DF1A61C5101}’) has children. In case if the folder does not exist or the folder does not have any children button should be hidden.

As we know visibility of button in ribbon which runs command dependent on a value returned by:

public override CommandState QueryState(CommandContext context)

So we’ll test exactly this method.

The code of command:

namespace CustomCommand
{
    using Sitecore.Data;
    using Sitecore.Shell.Framework.Commands;

    public class DoSomething : Command
    {
        public override void Execute(CommandContext context) { }

        public override CommandState QueryState(CommandContext context)
        {
            var folderId = new ID("{B4F0DAD0-B60E-49D9-8332-8DF1A61C5101}");
            var folder = Sitecore.Context.Database.GetItem(folderId);

            if (folder == null)
            {
                return CommandState.Hidden;
            }

            return folder.HasChildren ? CommandState.Enabled : CommandState.Hidden;
        }
    }
}

And the code of tests:

namespace CustomCommandTests
{
    using CustomCommand;
    using FluentAssertions;
    using NUnit.Framework;
    using Sitecore.Data;
    using Sitecore.FakeDb;
    using Sitecore.Shell.Framework.Commands;

    public class DoSomethingTests
    {
        [TestCase]
        public void CommandState_ShouldBeHidden_WhenFolderDoesNotExist()
        {
            // arrange
            var doSomethingCommand = new DoSomething();
            var commandContext = new CommandContext();

            using (var db = new Db { })
            {
                // act
                var commandState = doSomethingCommand.QueryState(commandContext);

                // assert
                commandState.Should().Be(CommandState.Hidden);
            }
        }

        [TestCase]
        public void CommandState_ShouldBeHidden_WhenNoChildren()
        {
            // arrange
            var folderId = new ID("{B4F0DAD0-B60E-49D9-8332-8DF1A61C5101}");
            var doSomethingCommand = new DoSomething();
            var commandContext = new CommandContext();

            using (var db = new Db
            {
                new DbItem("Folder", folderId)
            })
            {
                // act
                var commandState = doSomethingCommand.QueryState(commandContext);

                // assert
                commandState.Should().Be(CommandState.Hidden);
            }
        }

        [TestCase]
        public void CommandState_ShouldBeEnabled_WhenChildrenExist()
        {
            // arrange
            var folderId = new ID("{B4F0DAD0-B60E-49D9-8332-8DF1A61C5101}");
            var doSomethingCommand = new DoSomething();
            var commandContext = new CommandContext();

            using (var db = new Db
            {
                new DbItem("Folder", folderId)
                {
                    new DbItem("child 1"),
                    new DbItem("child 2")
                }
            })
            {
                // act
                var commandState = doSomethingCommand.QueryState(commandContext);

                // assert
                commandState.Should().Be(CommandState.Enabled);
            }
        }
    }
}

Note: for assertion in example above I use FluentAssertions.

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