You can make an extension method like below to Setup IMemoryCache to return data while unit testing in Xunit
public static void SetUpMemoryCacheToReturnData(this Mock<IMemoryCache> memoryCacheMockObject, object valueToBeReturned)
{
memoryCacheMockObject
.Setup(x => x.TryGetValue(It.IsAny<object>(), out valueToBeReturned))
.Returns(true);
}
Usage:
var valueToBeReturned = 1
_memoryCacheMockObject.SetUpMemoryCacheToReturnData(valueToBeReturned);
Namespaces used:
using Microsoft.Extensions.Caching.Memory;
using Moq;