Lea*_*rve 3 c# unit-testing moq mocking xunit
我知道 IMemoryCache.Set 是一个扩展方法,所以它不能被嘲笑。人们已经为这种情况提供了解决方法,例如 NKosi在这里提供的解决方法。我想知道如何为我的数据访问层实现这一点,其中我的 MemoryCache 返回一个值,当找不到它时,它从数据库中获取数据,将其设置为 MemoryCache 并返回所需的值。
public string GetMessage(int code)
{
if(myMemoryCache.Get("Key") != null)
{
var messages= myMemoryCache.Get<IEnumerable<MyModel>>("Key");
return messages.Where(x => x.Code == code).FirstOrDefault().Message;
}
using (var connection = dbFactory.CreateConnection())
{
var cacheOptions = new MemoryCacheEntryOptions { SlidingExpiration = TimeSpan.FromHours(1) };
const string sql = @"SELECT Code, Message FROM MyTable";
var keyPairValueData = connection.Query<KeyPairValueData>(sql);
myMemoryCache.Set("Key", keyPairValueData, cacheOptions );
return keyPairValueData.Where(x => x.Code == code).FirstOrDefault().Message;
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我的单元测试 - 当然它不起作用,因为我无法模拟 IMemoryCache
[Fact]
public void GetMessage_ReturnsString()
{
//Arrange
// Inserting some data here to the InMemoryDB
var memoryCacheMock = new Mock<IMemoryCache>();
//Act
var result = new DataService(dbConnectionFactoryMock.Object, memoryCacheMock.Object).GetMessage(1000);
//assert xunit
Assert.Equal("Some message", result);
}
Run Code Online (Sandbox Code Playgroud)
我要说的第一件事是为什么不使用真正的内存缓存?它会更好地验证行为,并且无需模拟它:
// Arrange
var memCache = new MemoryCache("name", new NameValueCollection());
//Act
var result = new DataService(dbConnectionFactoryMock.Object, memCache).GetMessage(1000);
// Assert: has been added to cache
memCache.TryGetValue("Key", out var result2);
Assert.Equal("Some message", result2);
// Assert: value is returned
Assert.Equal("Some message", result);
Run Code Online (Sandbox Code Playgroud)
如果你真的想模拟它,这里有一个关于如何做到这一点的指南:
因为它是一个扩展方法,所以您需要确保可以按原样调用它。在您的情况下,扩展方法将调用模拟。由于您没有提供预期的行为,它可能会失败。
您需要查看扩展方法的代码,检查它访问的内容,然后确保您的模拟符合预期的行为。代码可在此处获得:https : //github.com/aspnet/Caching/blob/master/src/Microsoft.Extensions.Caching.Abstractions/MemoryCacheExtensions.cs#L77
这是代码:
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, MemoryCacheEntryOptions options)
{
using (var entry = cache.CreateEntry(key))
{
if (options != null)
{
entry.SetOptions(options);
}
entry.Value = value;
}
return value;
}
Run Code Online (Sandbox Code Playgroud)
因此,从中您可以看到它访问CreateEnty并期望从中获得一个对象。然后它调用SetOptions并分配Value条目。
你可以像这样嘲笑它:
var entryMock = new Mock<ICacheEntry>();
memoryCacheMock.Setup(m => m.CreateEntry(It.IsAny<object>())
.Returns(entryMock.Object);
// maybe not needed
entryMock.Setup(e => e.SetOptions(It.IsAny<MemoryCacheEntryOptions>())
...
Run Code Online (Sandbox Code Playgroud)
执行此操作时,将在模拟上调用扩展方法并返回模拟条目。您可以修改实现并使其随心所欲。