public void GivenGetOrCreate_WhenNotExistsInDistributedCache_ThenInvokeFactory() { string cacheItem = "This is a test"; string key = "test"; bool factoryInvoked = false; _distributedCache.Setup(s => s.Get(key)).Returns(GetResult(null)); var response = _cachingService.GetOrCreate(key, () => { factoryInvoked = true; return(cacheItem); }); factoryInvoked.Should().BeTrue(); response.Should().Be(cacheItem); }