因此,我为ApiController构建了单元测试,其中之一仅在CI管道中的服务器上失败,但System.InvalidOperationException : HttpControllerContext.Configuration must not be null.
除外
结构如下:
public IHttpActionResult MyControllerMethod()
{
if(this.SomeThings()) // uses this.IService and this.IService2
return this.UnAuthorized();
DoOtherStuff();
return this.Ok();
}
在测试中,我模拟了IService和IService2并将它们传递给控制器:
var controllerUnderTest = new MyController(serviceMock, serviceMock2);
然后,我用一些无效的数据调用该方法,导致返回UnAuthorized()
:
var result = await controllerUnderTest.MyControllerMethod(invalidData).ExecuteAsync(CancellationToken.None);`
起初,这失败了。所以我加了
controllerUnderTest.Configuration = new HttpConfiguration();
controllerUnderTest.Request = new HttpRequestMessage();
所以现在可以了。在本地。它在调试时有效,在ReSharper的testrunner中有效,在NCrunch中有效。
但是它无法在管道中运行。
当使用ncrunch控制台工具在管道中执行测试时,测试失败并显示上述消息。
有人知道如何解决此问题吗?显式初始化config时如何使其为nul?
编辑:这是堆栈跟踪:
System.InvalidOperationException : HttpControllerContext.Configuration must not be null.
bei System.Web.Http.Results.ExceptionResult.ApiControllerDependencyProvider.EnsureResolved()
bei System.Web.Http.Results.ExceptionResult.ApiControllerDependencyProvider.get_IncludeErrorDetail()
bei System.Web.Http.Results.ExceptionResult.Execute()
bei System.Web.Http.Results.ExceptionResult.ExecuteAsync(CancellationToken cancellationToken)