Sitecore + TDD: bypassing system dependencies

Maybe be a case when your code dependents on system dependencies. E.g. your code can use DateTime.Now. The main idea of bypassing of such dependency is creating abstraction around last one. In the example below I wrap DateTime.Now in the protected virtual method:

        protected virtual DateTime GetDateTimeNow()
        {
            return DateTime.Now;
        }

On the one hand it gives us the ability to override this method in FakeExample2 class which is inherited from original one and substitute DateTime.Now with necessary value using FakeDateTimeNow property and on the other hand it does not change public API of Example2 class.

The context is the same with context from the previous blog post with one amendment – in current example button should be enabled if a folder contains any items which have Datetime field ‘Start Date’ with a value larger than DateTime.Now.

Code of command:

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

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

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

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

            if (folder.HasChildren
                && folder.Children.Any(c => DateUtil.IsoDateToDateTime(c["Start Date"]) > GetDateTimeNow()))
            {
                return CommandState.Enabled;
            }

            return CommandState.Hidden;
        }

        protected virtual DateTime GetDateTimeNow()
        {
            return DateTime.Now;
        }
    }
}

First test CommandState_ShouldBeHidden_WhenNoOneItemsContainStartDateLargerDateTimeNow :
– DateTime.Now set to 1st January 2018
– value of ‘Start Date’ field is 1st January 2017

Second text CommandState_ShouldBeEnabled_WhenExistItemContainsStartDateLargerDateTimeNow :
– DateTime.Now set to 1st January 2018
– value of ‘Start Date’ field is 31st December 2018

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

    public class Example2Tests
    {
        [TestCase]
        public void CommandState_ShouldBeHidden_WhenNoOneItemsContainStartDateLargerDateTimeNow()
        {
            // arrange
            var folderId = new ID("{B4F0DAD0-B60E-49D9-8332-8DF1A61C5101}");
            var doSomethingCommand = new FakeExample2 { FakeDateTimeNow = new DateTime(2018, 1, 1, 0, 0, 0) };
            var commandContext = new CommandContext();

            using (var db = new Db
            {
                new DbItem("Folder", folderId)
                {
                    new DbItem("child 1")
                    {
                        new DbField("Start Date") { Value = "20170101T000000" }
                    }
                }
            })
            {
                // act
                var commandState = doSomethingCommand.QueryState(commandContext);

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

        [TestCase]
        public void CommandState_ShouldBeEnabled_WhenExistItemContainsStartDateLargerDateTimeNow()
        {
            // arrange
            var folderId = new ID("{B4F0DAD0-B60E-49D9-8332-8DF1A61C5101}");
            var doSomethingCommand = new FakeExample2 { FakeDateTimeNow = new DateTime(2018, 1, 1, 0, 0, 0) };
            var commandContext = new CommandContext();

            using (var db = new Db
            {
                new DbItem("Folder", folderId)
                {
                    new DbItem("child 1")
                    {
                        new DbField("Start Date") { Value = "20181231T235959" }
                    }
                }
            })
            {
                // act
                var commandState = doSomethingCommand.QueryState(commandContext);

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

    public class FakeExample2 : Example2
    {
        public DateTime FakeDateTimeNow { get; set; }

        protected override DateTime GetDateTimeNow()
        {
            return FakeDateTimeNow;
        }
    }
}

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