How to Mock Rendering Parameters in Unit Test – Sitecore
- By : Mydatahack
- Category : Sitecore, Web Technologies
- Tags: Rendering Parameters, Sitecore, Unit Test
Now that we created a cool components with rendering parameters (see Using Rendering Parameters to Inject Data into Hidden Input), we can write a unit test. Sitecore’s Rendering class has a publicly accessible string field called Parameters. This is the rendering parameter string get populated on page rendering. Mocking is relatively straight forward as all you need to do is to add the mock parameters to this field.
Here is the controller created in the previous post. We will write a unit test on this.
Here are the unit tests, covering scenarios for parameters present and absent. As you can see, the RenderingParameters class is instantiated with a query string with parameters, then added to the Parameters field in the Rendering object.
All you need to do is to create rendering context with the RenderingContext.EnterContext method and pass the mock rendering.
We are using NSubstitue to mock the repository through its interface. This should be straight forward. We are also using FluentAssertions for assertions.
Here is the repository created in the previous post.
Unit testing it is super easy. Just create the RenderingParameters object as in the controller test. Then, compare the results.