November Happy Hour will be moved to Thursday December 5th.

Unit testing Headless backend (.net project)

Zuf
Zuf
Vote:
 

Hi,

Im new in the Optimizely world. 

I need some ideas/examples for how to make some unit tests of our backend Headless project. The main purpose is to get some more test coverage of the code.

Br. 

Zuf

#331887
Edited, Oct 24, 2024 5:46
Vote:
 

Hello Zuf,

I assume you are implementing Headless with CMS PAAS in this case and that you're already familiar with Unit Testing for .NET core.  I'm a big fan of unit testing my CMS implementations and my CMS AddOns.

If I can just give a few easy tips:

1. When testing search APIs, separate out the code that operates directly with the optimizely index code from your controllers and model building code.  The Optimizely Search & Navigation index code is not unit test friendly and I suspect (but cannot verify yet) that the same applies for graph.

2. When unit testing with dependencies injected via ServiceLocator, you will have extra steps to set up the mocked dependencies, but you can achieve it like so:

var contentAreaLoader = new Mock<IContentAreaLoader>();
var serviceProvider = new Mock<IServiceProvider>();
serviceProvider.Setup(x => x.GetService(typeof(IContentAreaLoader))).Returns(contentAreaLoader.Object);

ServiceLocator.SetServiceProvider(serviceProvider.Object);
#332440
Nov 05, 2024 9:06
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.