Testing an Umbraco Dictionary value using ICultureDictionary and UmbracoHelper

This week I finally found time to work on converting the Umbraco Unit Testing documentation from v8 to v9. The v9 version had some basic examples that I created a while back, but I want to bring all the examples from the v8 version in to the lastest version so I started this week by converting the UmbracoApiController example

This turned out to be ridiculously easy in v9, there's actually no mocking needed at all for this example (which is pretty basic). This PR have already been approved and merged. https://github.com/umbraco/UmbracoDocs/pull/3762

The next examples I wanted to convert was the Dictionary example, since this is something I know a lot of Umbracians use on a daily basis. So I thought I would share how it works in action.

Umbraco setup

For this example I have a document type called "Home". This document type has no properties at the moment, instead it only renders a dictionary value from the "Translation" tab, called "myDictionaryKey".

And then I have a HomeController that returns a custom viewmodel with a property called "MyDictionaryProperty" which I expect to be populated with my dictionary value.

Test setup

So here is what I ended up with: https://github.com/umbraco/UmbracoDocs/pull/3769/files

I am not actually testing if the underlying dictionary mechanism in Umbraco is working or if  Umbraco can fetch the dictionary value from the database/cache or not (that would be an Integration test).

I'm testing if the home page viewmodel property gets a dictionary value from the UmbracoHelper GetDictionaryValue() method, given that the underlying ICultureDictionaryFactory returns an expected value based on my key. I don't really care what that value is, which is why I use AutoFixture to generate a value. I just care that the same value returned from the ICultureDictionary is populated in my viewmodel.

Hope this will someday be useful to someone.

Cheers friends! ❤️