Home > .Net, C#, Unit Testing > Mocking Sitecore with Microsoft Fakes part 3

Mocking Sitecore with Microsoft Fakes part 3

This will be that last in this series Mocking Sitecore with Microsoft Fakes Links to Part 1 and Part 2 . To end end th series I will revisted a old blog post found here. In the original post I I created a TestItem, to test some old Production code. So in this post i will Test the same piece code.

First a view of the code we want to test.


 public class NavigationTitleFactory
 {
    public static string Create(Item item)
    {
      bool showInNavigation = item.GetCheckBoxValue(Constants.Fields.Navigable.ShowInMenu);

      if (!showInNavigation)
      {
         return string.Empty;
      }
      string navigationTitle = item.GetString(Constants.Fields.Navigable.Title);

      if (string.IsNullOrEmpty(navigationTitle))
        navigationTitle = item.Name;

      return navigationTitle;
    }
 }

With code above there a two ways we can test it. We could as in the original post create an Item mock using fakes, or we could overwrite the extension method something that is not possible with ordinary mocking frameworks.

Here below I will create an Item Mock.

[TestMethod]
  public void CreateItemWithShowInMenuFalseShouldReturnEmptyString()
  {

    string expectedNavigationTitle = "NavigationTitel";
    using (ShimsContext.Create())
    {
       Field showInMenu = new ShimField()
      {
        IDGet = () => Constants.Fields.Navigable.ShowInMenu,
        ValueGet = () => "0"
      };

      Field navigationTitle = new ShimField()
     {
       IDGet = () => Constants.Fields.Navigable.Title,
       ValueGet = () => expectedNavigationTitle
      };

     FieldCollection fieldCollection = new ShimFieldCollection()
     {
       ItemGetID = (id) => id == Constants.Fields.Navigable.ShowInMenu ? showInMenu : id ==                          Constants.Fields.Navigable.Title ? navigationTitle : null
     };

     Item itemStub = new ShimItem() { FieldsGet = () => fieldCollection };
     string actualNavigationTitle = NavigationTitleFactory.Create(itemStub);

     Assert.AreSame(string.Empty, actualNavigationTitle);
   }
 }

 [TestMethod]
 public void CreateItemWithShowInMenuTrueNoNavigationTitleShouldReturnItemName()
 {
   using (ShimsContext.Create())
   {
     Field showInMenu = new ShimField()
     {
       IDGet = () => Constants.Fields.Navigable.ShowInMenu,
       ValueGet = () => "1"
     };

     Field navigationTitle = new ShimField()
     {
       IDGet = () => Constants.Fields.Navigable.Title,
       ValueGet = () => string.Empty
      };

     FieldCollection fieldCollection = new ShimFieldCollection()
     {
       ItemGetID = id => id == Constants.Fields.Navigable.ShowInMenu ? showInMenu : id == Constants.Fields.Navigable.Title ? navigationTitle : null
     };

     string expectedItemName = "Name";
     Item itemStub = new ShimItem() { FieldsGet = () => fieldCollection, NameGet = () => expectedItemName };

     string actualNavigationTitle = NavigationTitleFactory.Create(itemStub);

     Assert.AreSame(expectedItemName, actualNavigationTitle);
   }
 }

 [TestMethod]
 public void CreateItemWithShowInMenuTrueShouldReturnItemNavigationTitle()
 {
   using (ShimsContext.Create())
  {
    string expectedNavigationTitle = "Navigation Title";
    Field showInMenu = new ShimField()
    {
      IDGet = () => Constants.Fields.Navigable.ShowInMenu,
      ValueGet = () => "1"
     };

     Field navigationTitle = new ShimField()
     {
       IDGet = () => Constants.Fields.Navigable.Title,
       ValueGet = () => expectedNavigationTitle
     };

    FieldCollection fieldCollection = new ShimFieldCollection()
    {
      ItemGetID = id => id == Constants.Fields.Navigable.ShowInMenu ? showInMenu : id == Constants.Fields.Navigable.Title ? navigationTitle : null
    };

    Item itemStub = new ShimItem
    {
      IDGet = () => ID.NewID,
      FieldsGet = () => fieldCollection,
    };

    string actualNavigationTitle = NavigationTitleFactory.Create(itemStub);

    Assert.AreSame(expectedNavigationTitle, actualNavigationTitle);
  }
}

Since the code only use extension methods to access item data it might make more sense to overwrite these methods to isolate any faulty implementations of item access in the extensions methods. This is shown below.

[TestMethod]
   public void EXTENSION_CreateItemWithShowInMenuFalseShouldReturnEmptyString()
   {
      using (ShimsContext.Create())
      {
        Item itemStub = new ShimItem();
        ShimItemExtensions.GetCheckBoxValueItemID = (item, id) => { return false; };
        string actualNavigationTitle = NavigationTitleFactory.Create(itemStub);

        Assert.AreSame(string.Empty, actualNavigationTitle);
      }
   }

  [TestMethod]
  public void EXTENSION_CreateItemWithShowInMenuTrueNoNavigationTitleShouldReturnItemName()
  {
     using (ShimsContext.Create())
     {
       string expectedItemName = "Name";
       Item itemStub = new ShimItem(){ NameGet = () => expectedItemName};
       ShimItemExtensions.GetCheckBoxValueItemID = (item, id) => true;
       ShimItemExtensions.GetStringItemID = (item, id) => string.Empty;

       string actualNavigationTitle = NavigationTitleFactory.Create(itemStub);

       Assert.AreSame(expectedItemName, actualNavigationTitle);
     }
  }

 [TestMethod]
 public void EXTENSION_CreateItemWithShowInMenuTrueShouldReturnItemNavigationTitle()
 {
    using (ShimsContext.Create())
    {
       string expectedNavigationTitle = "Navigation Title";
       Item itemStub = new ShimItem();

       ShimItemExtensions.GetCheckBoxValueItemID = (item, id) => true;
       ShimItemExtensions.GetStringItemID = (item, id) => expectedNavigationTitle;
       string actualNavigationTitle = NavigationTitleFactory.Create(itemStub);

       Assert.AreSame(expectedNavigationTitle, actualNavigationTitle);
    }
 }

The result of the test

part3a

This was the last part in this series. I hope this covers the most basic mocking of Sitecore, which until vs 2012 update wasn’t possible Otherwise let me know… 🙂

Categories: .Net, C#, Unit Testing Tags: , , ,
  1. Robert B
    06/11/2013 at 16:07

    I finally descovered what you mean, by adding fakes in VS2013 instead of VS2012. And I got all the examples working.
    Thanks for the help from your blog, Robert

    • 06/11/2013 at 16:36

      Glad you Got it to work

  1. No trackbacks yet.

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 )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: