コード例 #1
0
        public async Task Ctor_WhenRuleExpirationIsInvalidOrEmpty_ShouldUseDefaultExpiration()
        {
            // Arrange
            const RuleType ruleType      = RuleType.Action;
            var            cacheKey      = $"{_cacheKeysKey}_{ruleType}_{nameof(TestRule)}";
            var            cacheKeysKey  = _cacheKeysKey;
            var            expectedRules = new List <CompiledRule <TestRule> >();

            _innerRuleServiceMock.Setup(r => r.GetCompiledByRuleType <TestRule>(ruleType)).ReturnsAsync(expectedRules);

            object cacheRules;

            _cacheMock.Setup(c => c.TryGetValue(cacheKey, out cacheRules)).Returns(false);

            object cacheKeys;

            _cacheMock.Setup(c => c.TryGetValue(cacheKeysKey, out cacheKeys)).Returns(false);

            _cacheSettings = new CacheSettings {
                RuleExpiration = "invalid"
            };
            var appSettingsMock = new Mock <IOptions <CacheSettings> >();

            appSettingsMock.Setup(s => s.Value).Returns(_cacheSettings);

            _ruleService = new CacheableRuleService(_innerRuleServiceMock.Object, _cacheMock.Object, appSettingsMock.Object);

            // Act
            await _ruleService.GetCompiledByRuleType <TestRule>(RuleType.Action);

            // Assert
            _cacheMock.Verify(c => c.SetAsync(cacheKey, expectedRules, It.Is <CacheEntryOptions>(o => !o.Expiration.HasValue)));
        }
コード例 #2
0
        public async Task GetCompiledByRuleType_WhenInCache_ShouldGetRulesFromCache()
        {
            // Arrange
            const RuleType ruleType     = RuleType.Action;
            var            cacheKey     = $"{_cacheKeysKey}_{ruleType}_{nameof(TestRule)}";
            var            cacheKeysKey = _cacheKeysKey;

            object expectedRules = new List <CompiledRule <TestRule> >();

            _cacheMock.Setup(c => c.TryGetValue(cacheKey, out expectedRules)).Returns(true);

            object cacheKeys;

            _cacheMock.Setup(c => c.TryGetValue(cacheKeysKey, out cacheKeys)).Returns(false);

            // Act
            var compiledRules = await _ruleService.GetCompiledByRuleType <TestRule>(RuleType.Action);

            // Assert
            _innerRuleServiceMock.Verify(r => r.GetCompiledByRuleType <TestRule>(ruleType), Times.Never);
            compiledRules.Should().BeSameAs((List <CompiledRule <TestRule> >)expectedRules);
        }