ServiceStack (5.5.0) - 测试 ServiceStackController Gateway 为 null 并抛出异常时
ServiceStack (5.5.0) - When testing a ServiceStackController Gateway is null and throws an exception
使用 ServiceStack (v 5.5.0) 我阅读了通过控制器调用服务的推荐方法是使用网关。
完整示例位于 https://github.com/RhysWilliams647/ServiceStackControllerTest
public class HomeController : ServiceStackController
{
public ActionResult Index()
{
var response = Gateway.Send<TestServiceResponse>(new TestServiceRequest());
IndexModel model = new IndexModel { Message = response.Message };
return View(model);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
然而,当通过 xUnit 测试我的控制器时,我收到一个空异常错误,因为网关为空。下面是我的 AppHost
public class AppHost : AppSelfHostBase
{
public AppHost() : base("Test", typeof(TestService).Assembly)
{
}
public override IServiceGateway GetServiceGateway(IRequest req) =>
base.GetServiceGateway(req ?? new BasicRequest());
public override void Configure(Container container)
{
SetConfig(new HostConfig
{
HandlerFactoryPath = "api"
});
container.RegisterFactory<HttpContext>(() => HttpContext.Current);
// register container for mvc
ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
}
}
还有我的测试
[Trait("Category", "Controllers")]
[Collection("AppHostFixture")]
public class ControllerTest
{
[Fact]
public void CanCallHomeControllerIndex()
{
var controller = new HomeController();
controller.Index();
}
}
有人可以建议如何测试调用服务网关的 ServiceStackController 吗?
AppSelfHostBase
在 .NET Framework 上是一个不支持 MVC 的 HttpListener 自托管,因此您将无法 运行 任何集成测试。
当您像这样新建一个 MVC 控制器实例时:
var controller = new HomeController();
ServiceStackController
要求的 base.HttpContext
不存在,它需要一个 ASP.NET HttpContext 但单元测试中的自托管是 运行 自己-托管的 HttpListener 服务器。
您可以尝试通过 HostContext
单例访问网关,即:
var gateway = HostContext.AppHost.GetServiceGateway(new BasicRequest());
var response = gateway.Send<TestServiceResponse>(new TestServiceRequest());
在此示例中,它使用模拟 Http 请求上下文调用网关来模拟请求,但您将无法使用自托管 Http 侦听器执行真正的 MVC 集成测试。
使用 ServiceStack (v 5.5.0) 我阅读了通过控制器调用服务的推荐方法是使用网关。 完整示例位于 https://github.com/RhysWilliams647/ServiceStackControllerTest
public class HomeController : ServiceStackController
{
public ActionResult Index()
{
var response = Gateway.Send<TestServiceResponse>(new TestServiceRequest());
IndexModel model = new IndexModel { Message = response.Message };
return View(model);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
然而,当通过 xUnit 测试我的控制器时,我收到一个空异常错误,因为网关为空。下面是我的 AppHost
public class AppHost : AppSelfHostBase
{
public AppHost() : base("Test", typeof(TestService).Assembly)
{
}
public override IServiceGateway GetServiceGateway(IRequest req) =>
base.GetServiceGateway(req ?? new BasicRequest());
public override void Configure(Container container)
{
SetConfig(new HostConfig
{
HandlerFactoryPath = "api"
});
container.RegisterFactory<HttpContext>(() => HttpContext.Current);
// register container for mvc
ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
}
}
还有我的测试
[Trait("Category", "Controllers")]
[Collection("AppHostFixture")]
public class ControllerTest
{
[Fact]
public void CanCallHomeControllerIndex()
{
var controller = new HomeController();
controller.Index();
}
}
有人可以建议如何测试调用服务网关的 ServiceStackController 吗?
AppSelfHostBase
在 .NET Framework 上是一个不支持 MVC 的 HttpListener 自托管,因此您将无法 运行 任何集成测试。
当您像这样新建一个 MVC 控制器实例时:
var controller = new HomeController();
ServiceStackController
要求的 base.HttpContext
不存在,它需要一个 ASP.NET HttpContext 但单元测试中的自托管是 运行 自己-托管的 HttpListener 服务器。
您可以尝试通过 HostContext
单例访问网关,即:
var gateway = HostContext.AppHost.GetServiceGateway(new BasicRequest());
var response = gateway.Send<TestServiceResponse>(new TestServiceRequest());
在此示例中,它使用模拟 Http 请求上下文调用网关来模拟请求,但您将无法使用自托管 Http 侦听器执行真正的 MVC 集成测试。