What to use to write Unit Tests for Sitecore?

In this post, I would like to share with you some experiences I had in my last project and give you advice on what you can use to write Unit Tests and why sometimes some of the libraries are not a good way to go.

In the most cases when the team is thinking to start writing unit tests there is an discussion about different approaches and available libraries – been there done that.

Let me tell you my story

In our case, we had the following libraries taken into consideration:

  • NUnit vs XUnit
  • NSubstitute vs Moq
  • Asserts vs FluentAssertions
  • FakeDB

We totally forgot about AutoFixture and that was our mistake!

After couple of meetings (we had several teams that had to agree something) we decided to vote and the winners were:

  • NUnit
  • Moq
  • FluentAssertions
  • FakeDB

Of course, you may ask “why” … the answer is usually simple because team members knew these three or just liked to write unit tests with the support of these libraries than others.

Is this logical – in some way yes. Is it professional – some people may say ‘no’ but this is how real-world works. Software developers tend to use libraries that they know – especially when it comes to writing unit tests – even when there are better solutions out there.

Everything was fine at the beginning…

We started to write tests, we had around 100 test cases and then we realized that we need something to generate fake Sitecore items automatically.

We started to play we AutoFixture library and then we realized that we made a mistake …

AutoFixture was working really good with all of the libraries instead of FakeDB. We were able to generate simple types like strings etc. but no items were added to the FakeDB.

After some digging in google I found out that many people had that issue and there are two repeated solutions:

  • switch into XUnit
  • get rid of FakeDB and prepare your own fake objects

For us was too late – we decided to stay with our setup.

What to use – the answer

Based on my experience I would not start writing unit tests for Sitecore with NUnit in the future.

The rest of the listed libraries seem to be pretty safe and good from my point of view.

So the answer is:

Use XUnit and whatever else you want – but not NUnit because it can block you at some point as blocked me.

Sitecore SPEAK DatePicker issue with date selection

In this post I describe how to fix/workaround the issue with DatePicker component in Sitecore SPEAK 2.

Usually, DatePicker works just fine but at some point we noticed that something wrong is happening with date selection …

I identified two issues:

  • when we click the date in calendar it selects today rather than selected date
  • when we click the date in calendar it shows that we selected the date but value is set for today’s date not one that is shown

It might be related to the fact that I had to DatePicker components on a single page. Also, it might be related to the custom format of data that we use in the DatePicker – dd/MM/yyyy.

After some checks I decided to overwrite the vanilla DatePicker javascript with changed one.

Javascript file is in the path:

/sitecore/shell/client/Business Component Library/version 2/Layouts/Renderings/Common/DatePickers/DatePicker.js

I fixed it by adding ‘global’ variable to store set value: “definedDate”

Then I use that variable in two places.

First one is formatDate function that is called usually after selection

Second one is inside getISODate where we use it to calculate correct date

That change solves the incorrect behavior of the form.

Sitecore’s Personalization under the hood

About two months ago I had a pleasure to conduct webinar about personalization but this time I focused on something that people usually skip.

I did not describe how to setup the personalization rules but I tried to explain

  • what does it mean to apply a good personalization
  • why it is beneficial to apply personalization (for customers and companies)
  • how it works under the hood (algorithms and calucations process)

If you are interested in details you can watch it here (click image – you must to enter your email in the form to see the video):

You can also check details of presentation on slideshare:

I hope you like it – if you want to discuss it, please use comments section or reach me via social media.