1

I have below static class and method written to get the Sitecore Item by its path.

public static class MyService
    {
        public static Item GetItemByShortPath(SiteContext siteContext, string shortPath)
        {
             var fullPath = siteContext.StartPath + "/"+ shortPath;
            return siteContext.Database.GetItem(fullPath);
        }
}

Can someone please help me in writing unit test case for this code?

Thanks,

2 Answers 2

3

I can think of the below skeleton test.

    [TestClass]
    public class MyServiceTests
    {
        [TestMethod]
        public void GetItemByShortPath_Should_Return_Item()
        {
            // Arrange
            var database = new Db
            {
                new Database.GetItem("Home") { { "Title", "Welcome" } }
            };
    
            var siteContext = new SiteContext
            {
                StartPath = "/sitecore/content/Home",
                Database = database.Database
            };
    
            var shortPath = "Title";
    
            // Act
            var result = MyService.GetItemByShortPath(siteContext, shortPath);
    
            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("Welcome", result["Title"]);
        }
    
        [TestMethod]
        public void GetItemByShortPath_Should_Return_Null_When_Item_Not_Found()
        {
            // Arrange
            var database = new Db();
    
            var siteContext = new SiteContext
            {
                StartPath = "/sitecore/content/Home",
                Database = database.Database
            };
    
            var shortPath = "NonExistentItem";
    
            // Act
            var result = MyService.GetItemByShortPath(siteContext, shortPath);
    
            // Assert
            Assert.IsNull(result);
      

  }
}

In these test cases:

  1. GetItemByShortPath_Should_Return_Item: This test checks whether the GetItemByShortPath method returns a valid item when given a valid short path.

  2. GetItemByShortPath_Should_Return_Null_When_Item_Not_Found: This test checks whether the GetItemByShortPath method returns null when the item with the specified short path does not exist in the database.

Note:

1. Make sure to change the code according to your solution.

2. Make sure to adjust the test data and assertions as per your specific requirements and actual implementation.

Hope this might help.

2
1

I'd say FakeDb is outdated and I'd not recommend using it nowadays. Sitecore 9 brought abstractions so regular mocks can be utilized.

AutoFixture and Sitecore Customizations for AutoFixture help to reduce the amount of code needed to get started. See how the success scenario could look like:

[Theory, AutoNSubstituteData]
public void GetItemByShortPathReturnsExpectedIfFound(
    Database database, Item expected)
{
    var siteContext = new SiteContext(new SiteInfo(new StringDictionary
    {
        { "rootPath", "/sitecore/content/mysite" },
        { "startItem", "home" }
    }))
    {
        Database = database
    };
    database.GetItem("/sitecore/content/mysite/home/page1").Returns(expected);
    var actual = MyService.GetItemByShortPath(siteContext, "page1");
    Assert.Same(expected, actual);
}

Of course, there shold be more test to cover all the possible scenarions, e.g. what if database is null, what if /short-path (with extra slash) is passed instead of short-path etc. But, general idea should be clear.

In order to make it working, just copy few classes from here into your solution. The entire repo can be used as a reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.