回调未被调用
Callback not being called
所以我有以下接口/方法的实现:
public class CacheService : ICacheService
{
public T Get<T>(string cacheId, Func<T> getItemCallback) where T : class
{
var cacheExpiry = DateTime.UtcNow.AddMinutes(120);
var item = HttpRuntime.Cache.Get(cacheId) as T;
if (item == null)
{
item = getItemCallback();
HttpRuntime.Cache.Insert(cacheId, item, null, cacheExpiry, Cache.NoSlidingExpiration);
}
return item;
}
}
并且我正在尝试单元测试 (NUnit / Moq),给定一个空缓存(任何类型)
<Whatever> Callback
回调被调用一次;给定缓存服务的模拟实例和包含回调的服务。
方法的主体大致如下所示
return _services.Cache.GetBy("CacheKey", () => Callback("argumentsHere"));
然而,相反的是,它拒绝调用回调(如果我这样做,则测试通过)
return Callback("argumentsHere");
p.s。我这样设置测试
ICacheService cache = null;
_mockDomainServiceProvider.Setup(x => x.Cache).Returns(cache);
但如果我用 "real" CacheService
来设置它
ICacheService cache = new CacheService();
_mockDomainServiceProvider.Setup(x => x.Cache).Returns(cache);
它有效,但更多的是集成测试?
我没有使用过 mock,所以不知道它在 mock 时如何处理参数的来龙去脉,但你可以这样做来解决你的问题。
public class CacheService : ICacheService
{
public T Get<T>(string cacheId, Func<T> getItemCallback) where T : class
{
var cacheExpiry = DateTime.UtcNow.AddMinutes(120);
var item = HttpRuntime.Cache.Get(cacheId) as T;
if (item == null)
{
item = getItemCallback();
HttpRuntime.Cache.Insert(cacheId, item, null, cacheExpiry, Cache.NoSlidingExpiration);
}
return item;
}
protected virtual InsertCache(string key, Object value, CacheDependency dependencies,
DateTime absoluteExpiration, TimeSpan slidingExpiration) {
HttpRuntime.Cache.Insert(key, value, dependencies, absoluteExpiration,
Cache.slidingExpiration);
}
}
对获取 cacheExpiry 和 Item 的调用执行相同操作,现在在您的测试中,您可以创建一个 MockCacheService,它使用 public 用于 return cacheExpiry 和来自覆盖的受保护功能的项目。现在您已经完全控制了您的依赖项。另一种剥猫皮的方法。
所以我有以下接口/方法的实现:
public class CacheService : ICacheService
{
public T Get<T>(string cacheId, Func<T> getItemCallback) where T : class
{
var cacheExpiry = DateTime.UtcNow.AddMinutes(120);
var item = HttpRuntime.Cache.Get(cacheId) as T;
if (item == null)
{
item = getItemCallback();
HttpRuntime.Cache.Insert(cacheId, item, null, cacheExpiry, Cache.NoSlidingExpiration);
}
return item;
}
}
并且我正在尝试单元测试 (NUnit / Moq),给定一个空缓存(任何类型)
<Whatever> Callback
回调被调用一次;给定缓存服务的模拟实例和包含回调的服务。
方法的主体大致如下所示
return _services.Cache.GetBy("CacheKey", () => Callback("argumentsHere"));
然而,相反的是,它拒绝调用回调(如果我这样做,则测试通过)
return Callback("argumentsHere");
p.s。我这样设置测试
ICacheService cache = null;
_mockDomainServiceProvider.Setup(x => x.Cache).Returns(cache);
但如果我用 "real" CacheService
来设置它ICacheService cache = new CacheService();
_mockDomainServiceProvider.Setup(x => x.Cache).Returns(cache);
它有效,但更多的是集成测试?
我没有使用过 mock,所以不知道它在 mock 时如何处理参数的来龙去脉,但你可以这样做来解决你的问题。
public class CacheService : ICacheService
{
public T Get<T>(string cacheId, Func<T> getItemCallback) where T : class
{
var cacheExpiry = DateTime.UtcNow.AddMinutes(120);
var item = HttpRuntime.Cache.Get(cacheId) as T;
if (item == null)
{
item = getItemCallback();
HttpRuntime.Cache.Insert(cacheId, item, null, cacheExpiry, Cache.NoSlidingExpiration);
}
return item;
}
protected virtual InsertCache(string key, Object value, CacheDependency dependencies,
DateTime absoluteExpiration, TimeSpan slidingExpiration) {
HttpRuntime.Cache.Insert(key, value, dependencies, absoluteExpiration,
Cache.slidingExpiration);
}
}
对获取 cacheExpiry 和 Item 的调用执行相同操作,现在在您的测试中,您可以创建一个 MockCacheService,它使用 public 用于 return cacheExpiry 和来自覆盖的受保护功能的项目。现在您已经完全控制了您的依赖项。另一种剥猫皮的方法。