Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
GetAbsoluteExpirationValue() SonarQube report says, this method is partially covered(Partially covered by tests, 1 of 2 conditions).
But Resharper shows 100 % coverage.

Can you help me what am I missing here?


public MemoryCacheEntryOptions GetMemoryCacheEntryOptionsFromConfiguration()
        {
            var options = new MemoryCacheEntryOptions
            {
                AbsoluteExpiration = GetAbsoluteExpirationValue(),

                SlidingExpiration = GetSlidingExpirationValue()
            };

            return options;
        }

        private TimeSpan GetSlidingExpirationValue()
        {
            return TimeSpan.FromMinutes(
                double.TryParse(_configuration.GetValue<string>("CacheOptions:Menu:SlidingExpiration"),
                    out var slidingExpiration)
                    ? slidingExpiration
                    : 60); //default cache will expire in 60 minutes
        }

        private DateTimeOffset GetAbsoluteExpirationValue()
        {
            return DateTimeOffset.TryParse(
                _configuration.GetValue<string>("CacheOptions:Menu:AbsoluteExpiration"),
                out var absoluteExpiration)
                ? absoluteExpiration
                : DateTimeOffset.Now.AddMinutes(180); //default cache will expire in 180 minutes
        }


What I have tried:

[Fact]
       public void GetMemoryCacheEntryOptionsFromConfigurationShouldSetDefaultOptionsValuesWhenConfigurationValueIsInvalid()
       {
           var options = new MemoryCacheOptions();
           var memoryCache = new MemoryCache(options);
           var cacheService = new CacheService<string, string>(memoryCache, _configurationMock.Object);

           _configurationMock
               .Setup(c => c.GetSection("CacheOptions:Menu:AbsoluteExpiration").Value)
               .Returns("somewrongvalue");

           _configurationMock
               .Setup(c => c.GetSection("CacheOptions:Menu:SlidingExpiration").Value)
               .Returns("somewrongvalue");

           //Act
           var entryOptions = cacheService.GetMemoryCacheEntryOptionsFromConfiguration();

           // Assert
           entryOptions.SlidingExpiration.Should().Be(TimeSpan.FromMinutes(60));
           entryOptions.AbsoluteExpiration.HasValue.Should().BeTrue();
       }
Posted
Updated 30-Mar-23 1:15am

1 solution

ReSharper is wrong. You test what happens when the configuration contains an invalid value; but you never test what happens when the configuration contains a valid value.

There are four paths you need to test:
  1. CacheOptions:Menu:SlidingExpiration is invalid, CacheOptions:Menu:AbsoluteExpiration is invalid;
  2. CacheOptions:Menu:SlidingExpiration is invalid, CacheOptions:Menu:AbsoluteExpiration is valid;
  3. CacheOptions:Menu:SlidingExpiration is valid, CacheOptions:Menu:AbsoluteExpiration is invalid;
  4. CacheOptions:Menu:SlidingExpiration is valid, CacheOptions:Menu:AbsoluteExpiration is valid;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900