gpt4 book ai didi

c# - 单元测试ServiceStack缓存异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:34:46 25 4
gpt4 key购买 nike

我正在尝试对某些 ServiceStack 服务进行单元测试。这些服务使用缓存。我可以成功地模拟我的依赖项,并使用 MockRequestContext 调用我的服务就好了。但是当我的服务返回 base.RequestContext.ToOptimizedResultUsingCache 中的 DTO 时,我从下面的调用中得到一个空引用异常:

ServiceStack.CacheAccess.Providers.CacheClientExtensions.Cache(ICacheClient cacheClient, String cacheKey, Object responseDto, IRequestContext context, Nullable`1 expireCacheIn)

我的测试设置如下

[TestMethod]
public void GetAgencyCase_Returns_AgencyCaseDetailsResponse()
{
Container container = new Container();

Mock<IChangeRequestService> changeRequestService = new Mock<IChangeRequestService>();
Mock<IRequestService> requestService = new Mock<IRequestService>();

//Build the case we want returned
Case testCase = Builder<Case>.CreateNew()
.With(x => x.CaseId = "123")
.And(x => x.CaseNumber = "456")
.With(x => x.Agencies = Builder<CasesAgency>.CreateListOfSize(1)
.All()
.With(m => m.Agency = Builder<Agency>.CreateNew().With(z => z.AgencyId = 2).And(z => z.AgencyName = "Test").Build())
.Build())
.With(x => x.Requests = Builder<Request>.CreateListOfSize(5)
.Build())
.Build();

requestService.Setup<Case>(x => x.GetCase(It.IsAny<CaseSearchCriteria>(), It.IsAny<AuthenticatedUser>()))
.Returns(testCase);

container.Register<IChangeRequestService>(changeRequestService.Object);
container.Register<IRequestService>(requestService.Object);

container.Register<ILog>(new Mock<ILog>().Object);
container.Register<ICacheClient>(new MemoryCacheClient());
container.RegisterAutoWired<AgencyCaseService>();

var service = container.Resolve<AgencyCaseService>();
service.SetResolver(new BasicResolver(container));

var context = new MockRequestContext() { ResponseContentType = ContentType.Json };
context.CreateAppHost();

service.RequestContext = context;


var response = service.Get(new GetAgencyCase { AgencyId = 2, AgencyCaseNumber = "123" });
...assert stuff
}

当我调用我的服务方法时一切看起来都很好,但是当 dto 被保存到这里看到的缓存时我得到了空引用异常

try
{
var cacheKey = UrnId.Create<GetAgencyCase>(
request.AgencyId.ToString() +
request.AgencyCaseNumber);

var cacheExpireTime = TimeSpan.FromMinutes(_cacheDuration);

return base.RequestContext.ToOptimizedResultUsingCache<AgencyCaseDetailsResponse>(base.Cache, cacheKey, cacheExpireTime, () =>
{ ...business logic
return agencyCaseDto;
}

最佳答案

根据评论中的 mythz 建议,我添加了一个应用程序主机并使用了它的容器,测试成功了。以下是更新后的测试。

[TestMethod]
public void GetAgencyCase_Returns_AgencyCaseDetailsResponse()
{
var appHost = new BasicAppHost().Init();//this is needed for caching
Container container = appHost.Container;

Mock<IChangeRequestService> changeRequestService = new Mock<IChangeRequestService>();
Mock<IRequestService> requestService = new Mock<IRequestService>();

//Build the case we want returned
Case testCase = Builder<Case>.CreateNew()
.With(x => x.CaseId = "123")
.And(x => x.CaseNumber = "456")
.With(x => x.Agencies = Builder<CasesAgency>.CreateListOfSize(1)
.All()
.With(m => m.Agency = Builder<Agency>.CreateNew().With(z => z.AgencyId = 2).And(z => z.AgencyName = "Test").Build())
.Build())
.With(x => x.Requests = Builder<Request>.CreateListOfSize(5)
.Build())
.Build();

requestService.Setup<Case>(x => x.GetCase(It.IsAny<CaseSearchCriteria>(), It.IsAny<AuthenticatedUser>()))
.Returns(testCase);

container.Register<IChangeRequestService>(changeRequestService.Object);
container.Register<IRequestService>(requestService.Object);

container.Register<ILog>(new Mock<ILog>().Object);
container.Register<ICacheClient>(new MemoryCacheClient());
container.RegisterAutoWired<AgencyCaseService>();

var service = container.Resolve<AgencyCaseService>();
service.SetResolver(new BasicResolver(container));

var context = new MockRequestContext() { ResponseContentType = ContentType.Json };

service.RequestContext = context;


var response = service.Get(new GetAgencyCase { AgencyId = 2, AgencyCaseNumber = "123" });
...assert stuff
}

关于c# - 单元测试ServiceStack缓存异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19021130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com