public void CacheEnabledMethodSetsCorrectAbsoluteExpiration() { IServiceProvider serviceProvider = null; IUserService userService = null; var distributedCacheMock = new Mock <IDistributedCache>(); string cacheKey = null; DistributedCacheEntryOptions distributedCacheEntryOptions = null; distributedCacheMock.Setup(x => x.SetAsync( It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <DistributedCacheEntryOptions>(), It.IsAny <CancellationToken>() )) .Callback <string, byte[], DistributedCacheEntryOptions, CancellationToken>((key, b, cacheEntryOptions, c) => { if (!string.Equals(key, cacheKey)) { return; } distributedCacheEntryOptions = cacheEntryOptions; }) .Returns(Task.CompletedTask); "Given a service with a cache enabled method with an expiry".x(() => { serviceProvider = new ServiceCollection() .AddSingleton <CallCounter <UserService> >() .AddSingleton <IUserService, UserService>() .AddSingleton(distributedCacheMock.Object) .AddLogging() .AddMethodResultCaching() .BuildServiceProvider(); userService = serviceProvider.GetRequiredService <IUserService>(); }); "When calling the cache enabled method".x(async() => { var cacheKeyProvider = serviceProvider.GetRequiredService <CacheKeyProvider>(); cacheKey = await cacheKeyProvider.GetCacheKey(typeof(IUserService).GetMethod(nameof(IUserService.GetLastName)), new object[] { 1 }); userService.GetLastName(1); }); "Then the created cache entry should have the expected absolute expiration".x(() => { distributedCacheEntryOptions.Should().NotBeNull(); distributedCacheEntryOptions.SlidingExpiration.Should().Be( new TimeSpan( Constants.ExpirationHours, Constants.ExpirationMinutes, Constants.ExpirationSeconds ) ); }); }