My service code is below:
var cachedLatestVersion = await _cache.GetOrAddAsync("latestVersion", latestVersion, DateTimeOffset.Now.AddHours(24));
I have tried to mock the IAppache interface and set up a value for the GetOrAddAsync method but an error occurred
_cacheMock.Setup(c => c.GetOrAddAsync(It.IsAny<string>(), It.IsAny<Func<Task<string>>>(), It.IsAny<DateTimeOffset>())).ReturnsAsync("latestVersion");
The error:
System.NotSupportedException : Unsupported expression: c => c.GetOrAddAsync(It.IsAny(), It.IsAny<Func<Task>>(), It.IsAny()) Extension methods (here: AppCacheExtensions.GetOrAddAsync) may not be used in setup / verification expressions.
Then I have come up with another solution which is creating a new instance of MockCachingService():
var mockedCache = new MockCachingService(); var cacheMock = Mock.Get(mockedCache);
But I got another type of error
System.ArgumentException : Object instance was not created by Moq. (Parameter 'mocked')