Unit Test for Sitecore Custom Personalization Rule
I assume that in your career path you all become chance to hands on implementation for Sitecore Custom Personalization Rule. If you did not, then I have already described in detail about Custom Personalization Rule implementation in my one of previous blog. See the Blog Post: Custom Personalization Rules in Sitecore
Today I am going to share with you about implementation of Unit Test for Custom Personalization Rule.
Custom Personalization Rule business logic:
protected override bool Execute(T ruleContext)
{
if (_pageContext != null && !string.IsNullOrWhiteSpace(_pageContext.baseketValue))
{
var deliveryType = _basketDetailsService.GetBasket(_pageContext.basketValue).deliveryType;
if (deliveryType != null && deliveryType== DeliveryType.CashOnDelivery)
{
return true;
}
}
return false;
}
In above sample piece of code, I have created two Service Class type object using Constructor Dependency Injection named _pageContext and _basketDetailsService. From _pageContext service I will fetch the basket value field and will pass it to the another service method name GetBasket as a parameter, which will return me the delivery type of the product. In my personalization rule I am checking that if deliveryType is not null and deliveryType is equal to the CashOnDelivery then only return true else false.
Unit Test Case:
Before start with the Unit Test case, I assume that you have basic knowledge of implementation about Unit Test cases. I am not going to explain each test case scenario, I am writing only few test cases to give you gist about the way of implementation.
[TestMethod]
public void Execute_PageContextIsNull_ThenReturnFalse()
{
_mockPageContext = null;
var ruleStack = new RuleStack();
_sut.Evaluate(new RuleContext(), ruleStack);
ruleStack.Pop().Should().Be(false);
}
[TestMethod]
public void Execute_WhenDeliveryTypeIsCash_ThenReturnsFalse()
{
var deliveryType = DeliveryType.Cash;
_mockPageContext.Setup(x => x.basketValue).Returns("customer-xu78yb");
_mockBasketDetailsService.Setup(x => x. GetBasket("customer-xu78yb")).Returns(deliveryType);
var ruleStack = new RuleStack();
_sut.Evaluate(new RuleContext(), ruleStack);
ruleStack.Pop().Should().Be(false);
}
[TestMethod]
public void Execute_WhenDeliveryTypeIsCashOnDelivery_ThenReturnsTrue()
{
var deliveryType = DeliveryType.CashOnDelivery;
_mockPageContext.Setup(x => x.basketValue).Returns("customer-xu78yb");
_mockBasketDetailsService.Setup(x => x. GetBasket("customer-xu78yb")).Returns(deliveryType);
var ruleStack = new RuleStack();
_sut.Evaluate(new RuleContext(), ruleStack);
ruleStack.Pop().Should().Be(true);
}
Here is above example you can see that Sitecore Rules "WhenCondition" provide two methods Evaluate and Execute. Sitecore will create RuleStack to Pushed the result of each condition we write in Execute method and we passed the rule stack and context of our condition to Evaluate method as a parameter. Once evaluate method will execute it will return the expected result in stack based on the context we passed in method. From the stack using pop method we can check the result with the help of Assertion.
Happy Sitecoreing 😊
Unit Test for Sitecore Custom Personalization Rule (will check this rules..)
ReplyDeleteKeep posting...
Sitecore Development Company
Sitecore CMS Development Services