我正在尝试对在内存缓存中使用的ServiceStack服务进行单元测试。我正在使用MockRequestContext,当我点击返回base.RequestContext.ToOptimizedResultUsingCache时,我得到一个超出范围异常的索引。我不确定缺少什么。是否可以使用此模拟请求上下文对使用缓存的服务进行单元测试?
测试代码
[TestMethod]
public void GetAgencyCase_Returns_AgencyCaseDetailsResponse()
{
var service = _container.Resolve<AgencyCaseService>();
service.SetResolver(new BasicResolver(_container));
service.RequestContext = new MockRequestContext();
var response = service.Get(new GetAgencyCase { AgencyId = 2, AgencyCaseNumber = "123" });
}
调用缓存优化了我正在测试的服务中的扩展方法。
return base.RequestContext.ToOptimizedResultUsingCache<AgencyCaseDetailsResponse>(base.Cache, cacheKey, cacheExpireTime, () =>
{...
来自IndexOutOfRangeException的堆栈跟踪
at ServiceStack.Common.Web.ContentType.IsBinary(String contentType)
at ServiceStack.CacheAccess.Providers.CacheClientExtensions.ResolveFromCache(ICacheClient cacheClient, String cacheKey, IRequestContext context)
at ServiceStack.ServiceHost.RequestContextExtensions.ToOptimizedResultUsingCache[T](IRequestContext requestContext, ICacheClient cacheClient, String cacheKey, Nullable`1 expireCacheIn, Func`1 factoryFn)
答案 0 :(得分:3)
我弄明白了这个问题。您必须为请求提供响应内容类型,以便缓存调用起作用。修复如下:
service.RequestContext = new MockRequestContext() { ResponseContentType = ContentType.Json };