Mock IMemoryCache in unit test(在单元测试中模拟 IMemoryCache)
问题描述
我正在使用 asp net core 1.0 和 xunit.
I am using asp net core 1.0 and xunit.
我正在尝试为一些使用 IMemoryCache
的代码编写单元测试.但是,每当我尝试在 IMemoryCache
中设置一个值时,我都会收到 Null 引用错误.
I am trying to write a unit test for some code that uses IMemoryCache
. However whenever I try to set a value in the IMemoryCache
I get an Null reference error.
我的单元测试代码是这样的:IMemoryCache
被注入到我要测试的类中.但是,当我尝试在测试中的缓存中设置一个值时,我得到一个空引用.
My unit test code is like this:
The IMemoryCache
is injected into the class I want to test. However when I try to set a value in the cache in the test I get a null reference.
public Test GetSystemUnderTest()
{
var mockCache = new Mock<IMemoryCache>();
return new Test(mockCache.Object);
}
[Fact]
public void TestCache()
{
var sut = GetSystemUnderTest();
sut.SetCache("key", "value"); //NULL Reference thrown here
}
这是Test类...
public class Test
{
private readonly IMemoryCache _memoryCache;
public Test(IMemoryCache memoryCache)
{
_memoryCache = memoryCache;
}
public void SetCache(string key, string value)
{
_memoryCache.Set(key, value, new MemoryCacheEntryOptions {SlidingExpiration = TimeSpan.FromHours(1)});
}
}
我的问题是...我需要以某种方式设置 IMemoryCache
吗?为 DefaultValue 设置一个值?当 IMemoryCache
被 Mocked 时,默认值是多少?
My question is...Do I need to setup the IMemoryCache
somehow? Set a value for the DefaultValue? When IMemoryCache
is Mocked what is the default value?
推荐答案
IMemoryCache.Set
是扩展方法,因此无法使用 Moq 框架.
IMemoryCache.Set
Is an extension method and thus cannot be mocked using Moq framework.
扩展的代码虽然可用 这里
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;
}
对于测试,需要通过扩展方法模拟一条安全路径,以使其顺利完成.在 Set
中,它还调用缓存条目上的扩展方法,因此也必须满足这一要求.这会很快变得复杂,所以我建议使用具体的实现
For the test, a safe path would need to be mocked through the extension method to allow it to flow to completion. Within Set
it also calls extension methods on the cache entry, so that will also have to be catered for. This can get complicated very quickly so I would suggest using a concrete implementation
//...
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
//...
public Test GetSystemUnderTest() {
var services = new ServiceCollection();
services.AddMemoryCache();
var serviceProvider = services.BuildServiceProvider();
var memoryCache = serviceProvider.GetService<IMemoryCache>();
return new Test(memoryCache);
}
[Fact]
public void TestCache() {
//Arrange
var sut = GetSystemUnderTest();
//Act
sut.SetCache("key", "value");
//Assert
//...
}
所以现在您可以访问功能齐全的内存缓存了.
So now you have access to a fully functional memory cache.
这篇关于在单元测试中模拟 IMemoryCache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在单元测试中模拟 IMemoryCache


- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 输入按键事件处理程序 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01