Owin 测试(自托管)在使用 DI 时未注册网络 api 路由
Owin Testing (Self hosting) not registering web api routes when using DI
我正在尝试实施 Microsoft.Owin.Testing/3.0.1 for WebApi 2 integration tests. Everything works fine except when I add attribute routing。路线无法识别或我无法正确注册。我很确定它与测试时未使用的 api 控制器依赖注入有关。
我的价值观api控制器
[RoutePrefix("api")]
public class ValuesApiController : ApiController
{
private readonly ValuesControllerService _valuesControllerService;
public ValuesApiController(ValuesControllerService valuesControllerService)
{
_valuesControllerService = valuesControllerService;
}
// GET api/values
[HttpGet]
[Route("values")]
public async Task<IHttpActionResult> Get()
{
if (_valuesControllerService == null)
throw new ArgumentException("valuesControllerService");
return Ok(new[] { "value1", "value2" });
}
}
我正在使用 Castle Windsor 解决 API 控制器使用 (source):
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new WindsorCompositionRoot(_container));
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container));
我正在 Global.asax 中注册我的 WebApi 路由,因此当 运行 应用程序(而不是测试)时它被正确注册。
public class WebApiApplication : HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
测试的时候,我在Startup之后调用注册。这是我使用的示例测试 (test source):
[Test]
public async void GetValueTestForStartup1()
{
using (var server = TestServer.Create<Startup1>())
{
using (var client = new HttpClient(server.Handler))
{
var request = await client.GetAsync(_url);
Assert.IsTrue(request.IsSuccessStatusCode);
}
}
}
下面是我尝试过的一些 Startup 实现:
internal class Startup1
{
public void Configuration(IAppBuilder app)
{
new Startup().ConfigureForIntegrationTests(app, x => x.Kernel.ComponentModelBuilder.AddContributor(new SingletonEqualizer()));
GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver), new TestWebApiResolver());
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
404 when executing a request
internal class Startup2
{
public void Configuration(IAppBuilder app)
{
new Startup().ConfigureForIntegrationTests(app, x => x.Kernel.ComponentModelBuilder.AddContributor(new SingletonEqualizer()));
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configuration.EnsureInitialized();
}
}
System.InvalidOperationException : This method cannot be called during
the application's pre-start initialization phase.
internal class Startup3
{
public void Configuration(IAppBuilder app)
{
new Startup().ConfigureForIntegrationTests(app, y => y.Kernel.ComponentModelBuilder.AddContributor(new SingletonEqualizer()));
// Web API routes
GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
app.UseWebApi(GlobalConfiguration.Configuration);
GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver), new TestWebApiResolver());
}
}
500 Internal Server error (probably because there is no parameterless
constructor found).
完整的源项目位于此处:
https://github.com/jvanderbiest/OwinTestingWebApi
有一个类似的 SO 问题 here about AutoFac. I must have looked over that response 但所有这一切的关键是用您自己的 HttpConfiguration
注册所有内容,而不是使用 GlobalConfiguration
对象。
我已经更新了 github code 所以测试现在工作正常!
我正在尝试实施 Microsoft.Owin.Testing/3.0.1 for WebApi 2 integration tests. Everything works fine except when I add attribute routing。路线无法识别或我无法正确注册。我很确定它与测试时未使用的 api 控制器依赖注入有关。
我的价值观api控制器
[RoutePrefix("api")]
public class ValuesApiController : ApiController
{
private readonly ValuesControllerService _valuesControllerService;
public ValuesApiController(ValuesControllerService valuesControllerService)
{
_valuesControllerService = valuesControllerService;
}
// GET api/values
[HttpGet]
[Route("values")]
public async Task<IHttpActionResult> Get()
{
if (_valuesControllerService == null)
throw new ArgumentException("valuesControllerService");
return Ok(new[] { "value1", "value2" });
}
}
我正在使用 Castle Windsor 解决 API 控制器使用 (source):
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new WindsorCompositionRoot(_container));
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container));
我正在 Global.asax 中注册我的 WebApi 路由,因此当 运行 应用程序(而不是测试)时它被正确注册。
public class WebApiApplication : HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
测试的时候,我在Startup之后调用注册。这是我使用的示例测试 (test source):
[Test]
public async void GetValueTestForStartup1()
{
using (var server = TestServer.Create<Startup1>())
{
using (var client = new HttpClient(server.Handler))
{
var request = await client.GetAsync(_url);
Assert.IsTrue(request.IsSuccessStatusCode);
}
}
}
下面是我尝试过的一些 Startup 实现:
internal class Startup1
{
public void Configuration(IAppBuilder app)
{
new Startup().ConfigureForIntegrationTests(app, x => x.Kernel.ComponentModelBuilder.AddContributor(new SingletonEqualizer()));
GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver), new TestWebApiResolver());
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
404 when executing a request
internal class Startup2
{
public void Configuration(IAppBuilder app)
{
new Startup().ConfigureForIntegrationTests(app, x => x.Kernel.ComponentModelBuilder.AddContributor(new SingletonEqualizer()));
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configuration.EnsureInitialized();
}
}
System.InvalidOperationException : This method cannot be called during the application's pre-start initialization phase.
internal class Startup3
{
public void Configuration(IAppBuilder app)
{
new Startup().ConfigureForIntegrationTests(app, y => y.Kernel.ComponentModelBuilder.AddContributor(new SingletonEqualizer()));
// Web API routes
GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
app.UseWebApi(GlobalConfiguration.Configuration);
GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver), new TestWebApiResolver());
}
}
500 Internal Server error (probably because there is no parameterless constructor found).
完整的源项目位于此处: https://github.com/jvanderbiest/OwinTestingWebApi
有一个类似的 SO 问题 here about AutoFac. I must have looked over that response 但所有这一切的关键是用您自己的 HttpConfiguration
注册所有内容,而不是使用 GlobalConfiguration
对象。
我已经更新了 github code 所以测试现在工作正常!