Controller test fails only on server

101 Views Asked by At

So I built unit tests for an ApiController, and one of those fails only on the server in the CI pipeline with the exception System.InvalidOperationException : HttpControllerContext.Configuration must not be null.

The structure looks like this:

public IHttpActionResult MyControllerMethod()
{
  if(this.SomeThings()) // uses this.IService and this.IService2
    return this.UnAuthorized();

  DoOtherStuff();
  return this.Ok();
}

In my test, I mock IService and IService2 and pass them to the controller:

var controllerUnderTest = new MyController(serviceMock, serviceMock2);

Then I call the method with some invalid data, provoking the return of UnAuthorized():

var result = await controllerUnderTest.MyControllerMethod(invalidData).ExecuteAsync(CancellationToken.None);`

At first, this failed. So I added

controllerUnderTest.Configuration = new HttpConfiguration();
controllerUnderTest.Request = new HttpRequestMessage();

from this Stackoverflow post.

So now it works. Locally. It works when debugging, it works using ReSharper's testrunner, it works using NCrunch.

But it doesn't work in the pipeline.
When executed in the pipeline using ncrunch console tool, the tests fails with above message.

Any idea how to fix this? How can config be nul when I explicitely initialize it?

Edit: Here is the stacktrace:

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) 
0

There are 0 best solutions below