alastairtree / LazyCache

An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
https://nuget.org/packages/LazyCache
MIT License
1.72k stars 159 forks source link

Mocking IAppCache with AppCacheExtenions Extension Methods #88

Closed justinkneff closed 4 years ago

justinkneff commented 5 years ago

In version .7.1.44 there was the ability to use Moq to mock the values of the IAppCache because the implementation was on the CachingService.. When upgrading to 2.0.1 that implementation is now extension methods inside of AppCacheExtensions. Looking at the Unit Tests there doesn't appear to be anything mocking the setup because it does the full implementation using the BuildCache() function in the Setup of the unit tests. Is there a way to use mocking of the Interface without extension methods requiring the implementation of the actual service?

alastairtree commented 4 years ago

Sorry for the slow reply - have been deidicating more time to other projects this year. Hopfully you solved this yourself by now but will reply here for others to find later.

When I unit test code using LazyCache I tend to just use a real instance of the CachingService since it has no external dependencies and it is usually part of the thing I want to test. However if I really need to isolate the caching I use the MockCachingService() implementation in the LazyCache.Mocks namespace. See the Unit Testing docs for more info.