在单元测试中模拟IMemoryCache [英] Mock IMemoryCache in unit test

查看:70
本文介绍了在单元测试中模拟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类...

And this is the class 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被模拟时,默认值是什么?

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是扩展方法,因此无法使用

IMemoryCache.Set Is an extension method and thus cannot be mocked using Moq framework.

该扩展的代码虽然可用

The code for the extension though is available here

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆