This is the final post in my “Unit Testing Sitecore Components” series. In this series I’ve taken a seemingly simplistic Sitecore component and refactored it by applying several principals to make the code more reusable and testable. In this final post of the series I’ll recap the principals and provide a few resources to help explain them further.
In the previous posts of this series, I’ve refactored an existing Sitecore component to make it’s logic more reusable and prepare the component and the logic it includes for unit testing. In this post I’ll be writing the unit tests for the refactored EntryTaxonomy class, showing how to mock items and field values.
In the posts of this series so far, I’ve refactored a sample view rendering to make the rendering itself unit testable. In doing do I’ve extracted the business logic to a separate class which still needs some work before it can be properly tested. In this post, I’ll be covering 2 more principals to help with making Sitecore components more testable and reusable.
In the previous post of this series I detailed 2 principals which can help with making Sitecore components more testable and reusable. These were “keeping the business logic out of the view” and “keeping the Item class out of the model”. In this post I’ll detail several more principals to continue improving the code.
At the end of April (2020), I had the great pleasure of presenting at the virtual SUGCON conference online. My presentation was a live walkthrough showing how to unit test a Sitecore component. In the presentation I utilised many principals to refactor and improve a view rendering.
Back in January I wrote an article about how to Roll your own Sitecore Item, which showed how to create an in-memory Sitecore item for use in testing. The technique was quite limited and still required some small config entry to allow everything to work (to create a Database instance). Well, Sitecore 8.2 has finally made a reality what so many of us have prayed for, for so long. We can finally mock Items and Databases with Sitecore 8.2. Though, it’s probably not quite as simple as you might hope.
A few years back, I uploaded a couple of short videos to YouTube to show how easy it can be to get started with testing your Sitecore solution. The approach shown in the videos uses my ASP.NET embedded test runner. I developed the first version of this test runner when I was first trying to test my own Sitecore projects, many years ago. The issue many developers face when trying to test their Sitecore solutions is trying to get the Sitecore code to run outside of a web/Sitecore context. What I managed to do with this test runner was flip the problem on it’s head; instead of trying to get the Sitecore code to work inside my unit test, I made my unit test work inside my Sitecore solution.
One of the most difficult aspects of unit testing against Sitecore is mocking Sitecore items. The Sitecore Item class is quite a complex class and it’s interface is not structured the way most mocking frameworks require. Mocking frameworks allow you to create dummy objects for use inside tests. But that’s not the only way to create dummy objects. One can always instantiate a class if the classes interface allows it. You might be surprised to learn that you can in fact create an instance of a Sitecore Item in your own code.