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