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
基础教程推荐
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
