I am experimenting with Unit Tests on our Umbraco 13 solution and am having trouble creating a Moq umbracoHelper object that actually returns a sample value, for example, Umbraco.GetDictionaryValue(“Home”).
The documentation here gives an example but the GetDictionaryValue is called in the method I want to test, and I can’t get it to work. Any guidance would be very helpful.
To mock UmbracoHelper and specifically dictionary keys you need to provide your mocked UmbracoHelper with ICultureDictionaryFactory, IUmbracoComponentRenderer , and IPublishedContentQuery
Dictionary keys are setup via the ICultureDictionary which in turn is setup in the ICultureDictionaryFactory in the CreateDictionary() method.
See the provided example which should work
Mock<ICultureDictionary> cultureDictionary = new Mock<ICultureDictionary>();
cultureDictionary.Setup(x => x["MY_KEY"])
.Returns("expectedValue");
Mock<ICultureDictionaryFactory> cultureDictionaryFactory = new Mock<ICultureDictionaryFactory>();
cultureDictionaryFactory.Setup(x => x.CreateDictionary()).Returns(cultureDictionary.Object);
Mock<UmbracoHelper> umbracoHelper = new Mock<UmbracoHelper>(cultureDictionaryFactory.Object, Mock.Of<IUmbracoComponentRenderer>(), Mock.Of<IPublishedContentQuery>());