如何测试 CacheProvider
How to test a CacheProvider
我有一个缓存 class 如下:
public class MyCache : ICacheProvider
{
private readonly IMemoryCache _cache;
private readonly MemoryCacheOptions _options;
private readonly ILogger<InMemoryCacheProvider> _logger;
public MyCache(IMemoryCache cache, MemoryCacheOptions options, ILogger<InMemoryCacheProvider> logger)
{
//Elided
}
public virtual void Set<T>(string key, T value, TimeSpan expiration) where T : class
{
_cache.Set(key, value, expiration);
}
public virtual T Get<T>(string key) where T : class
{
if (_cache.Get(key) is T result)
{
return result;
}
return default(T);
}
// removed some code for clarity
}
ICacheProvider 有类似 Set
和 Get
的方法。
我该如何测试这个 class?我需要测试 set 方法是否真的为依赖项设置了一些东西。使用 FakeitEasy,我完成了以下操作:
[Fact]
public void SetTest()
{
var cache = A.Fake<MyCache>();
var item = A.Fake<TestClass>();
cache.Set("item", item);
A.CallTo(() => cache.Set("item", item)).MustHaveHappened();
}
但这对我来说意义不大。
我感兴趣的是,当我调用 set 方法时,我需要能够检查假缓存中是否真的有一个对象集或其他什么。 Get 和其他方法相同。
能否详细说明一下?
@Nkosi 的评论是正确的。通过模拟被测系统的协作者来使用模拟框架。然后就可以运行被测系统了。像这样:
// mock a collaborator
var fakeCache = A.Fake<IMemoryCache>();
// Create a real system under test, using the fake collaborator.
// Depending on your circumstances, you might want real options and logger,
// or fake options and logger. For this example, it doesn't matter.
var myCacheProvider = new MyCache(fakeCache, optionsFromSomewhere, loggerFromSomewhere);
// exercise the system under test
myCacheProvider.Set("item", item, someExpriation);
// use the fake collaborator to verify that the system under test worked
// As @Nkosi points out, _cache.Set(key, value, expiration)
// from the question is an extension method, so you can't assert on it
// directly. Otherwise you could do
// A.CallTo(() => fakeCache.Set("item", item, expiration)).MustHaveHappened();
// Instead you'll need to assert on CreateEntry, which could be a little trickier
我有一个缓存 class 如下:
public class MyCache : ICacheProvider
{
private readonly IMemoryCache _cache;
private readonly MemoryCacheOptions _options;
private readonly ILogger<InMemoryCacheProvider> _logger;
public MyCache(IMemoryCache cache, MemoryCacheOptions options, ILogger<InMemoryCacheProvider> logger)
{
//Elided
}
public virtual void Set<T>(string key, T value, TimeSpan expiration) where T : class
{
_cache.Set(key, value, expiration);
}
public virtual T Get<T>(string key) where T : class
{
if (_cache.Get(key) is T result)
{
return result;
}
return default(T);
}
// removed some code for clarity
}
ICacheProvider 有类似 Set
和 Get
的方法。
我该如何测试这个 class?我需要测试 set 方法是否真的为依赖项设置了一些东西。使用 FakeitEasy,我完成了以下操作:
[Fact]
public void SetTest()
{
var cache = A.Fake<MyCache>();
var item = A.Fake<TestClass>();
cache.Set("item", item);
A.CallTo(() => cache.Set("item", item)).MustHaveHappened();
}
但这对我来说意义不大。
我感兴趣的是,当我调用 set 方法时,我需要能够检查假缓存中是否真的有一个对象集或其他什么。 Get 和其他方法相同。
能否详细说明一下?
@Nkosi 的评论是正确的。通过模拟被测系统的协作者来使用模拟框架。然后就可以运行被测系统了。像这样:
// mock a collaborator
var fakeCache = A.Fake<IMemoryCache>();
// Create a real system under test, using the fake collaborator.
// Depending on your circumstances, you might want real options and logger,
// or fake options and logger. For this example, it doesn't matter.
var myCacheProvider = new MyCache(fakeCache, optionsFromSomewhere, loggerFromSomewhere);
// exercise the system under test
myCacheProvider.Set("item", item, someExpriation);
// use the fake collaborator to verify that the system under test worked
// As @Nkosi points out, _cache.Set(key, value, expiration)
// from the question is an extension method, so you can't assert on it
// directly. Otherwise you could do
// A.CallTo(() => fakeCache.Set("item", item, expiration)).MustHaveHappened();
// Instead you'll need to assert on CreateEntry, which could be a little trickier