Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
public ProductConfigDetails GetProductConfig()
           ProductConfigDetails ProdConfigDetails;

           if (appsettingsconfiguration.UseMockData)
               taxConfigDetails = JsonFileReader.GetJsonFileData<TaxConfigDetails>("ProductConfiguration.json");
               return ProdConfigDetails;
               var lastModifieddteDateTime = System.IO.File.GetLastWriteTime("C:\\Data\\ProductConfiguration.json");
               _memoryCache.TryGetValue("lastedit", out DateTime? modifiedDate);
               if (lastModifieddteDateTime != modifiedDate)
                   var config = System.IO.File.ReadAllText("C:\\Data\\ProductConfiguration.json");
                   ProdConfigDetails = JsonConvert.DeserializeObject<ProductConfigDetails>(config);
                   _memoryCache.Set("lastedit", lastModified);
                   _memoryCache.Set("cachedConfig", ProdConfigDetails);
                   return ProdConfigDetails;
                   _memoryCache.TryGetValue("cachedConfig", out ProdConfigDetails);
                   return ProdConfigDetails;

What I have tried:

Job of this method: Fetch and return the JSON file details.
if option set to read mock data it is going to fetch from soln local mocked file.
else read the file from filesystem and then cache it.
if cached data is unchanged, fetch from cache.

For this method I need to write unit tests.
what are the possible tests I can write for this method?

1) GetProdConfigDetails_FromPhysicalPath()
2) GetProdConfigDetails_FromMockFile()
3) GetProdConfigDetails_FromInMemoryCache()

Please feel free to comment if any other test needs to be added.

How can I implement this test GetProdConfigDetails_FromInMemoryCache()?
Updated 11-Aug-22 19:56pm
George Swan 12-Aug-22 4:11am    
It seems to me that you do not need any of the 'else' statements as all the previous 'if' statements exit the method if they are true. The method does not adhere to the single responsibility principle and that is one reason why it is difficult to test. I would suggest a separate method for each of the three tasks you have listed above

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