Sitecore UnitTest with FakeDb
When you are working on the CMS solution, the solution design and architecture can be very different, that caused writing and maintaining the uint tests be even more difficult than bespoke solution.
A typical example of this is at some point your code calls the Sitecore API, how do you ensure your code is working when the code has related to external dependencies that you can’t remove?
Thanks for Sitecore FakeDb, it makes that happen and a lot simpler to write the unit tests on this CMS plateform. In the following sections, I will assume you are a Sitecore developer with passion of writing the unit tests.
In order to use FakeDb for the unit tests, you need the following dlls within your Tests project
- Sitecore.FakeDb – You can install this via the nuget package
NOTES: All above Sitecore related packages can be either reference from dll or via Sitecore nuget server.
Lastly, you also need to copy your Sitecore license.xml file into the root of your Test project.
NOTE: When you install the Sitecore.FakeDb package, it will automatically update the app.config file by including the license file location (see screenshot below
Sample Test (MSTest)
In order to run unit tests with NCrunch, in the NCrunch Configuration window, select the tests project and configure the following settings:
AutoFixture with Sitecore.FakeDb
Happy Coding! 😇