Unit testing WebApi 2 ApiController with ExecuteAsync overriden

I have an ApiController with a method ExecuteAsync

overridden like this (I am using RavenDB and create its session here):

public override async Task<HttpResponseMessage> ExecuteAsync(
        HttpControllerContext controllerContext,
        CancellationToken cancellationToken)
{
    using (Session = Store.OpenAsyncSession())
    {
        var result = await base.ExecuteAsync(controllerContext, cancellationToken);
        await Session.SaveChangesAsync();
        return result;
    }
}

      

I want to write automated tests for it using xUnit or Microsoft Test Framework and at the moment I am stuck with calling controllers using ExecuteAsync.

Do I have to construct the ControllerContext in some specific way in order to do this, or am I missing something?

Simply creating a controller object and calling actions throws a NullReferenceException for the Session object, which is not explicitly created because ExecuteAsync was not called.

+3


source to share





All Articles