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
unit-testing asp.net-web-api testing asp.net-web-api2 ravendb


source to share


No one has answered this question yet

Check out similar questions:

2560
How can I test a private function or class with private methods, fields or inner classes?
830
Unit Testing C Code
730
What's the difference between unit, functional, acceptance, and integration tests?
717
JavaScript unit test tools for TDD
681
What is Unit test, integration test, Smoke test, regression test?
646
How do I unit test threaded code?
573
Is unit testing worth the effort?
507
Writing Unit Tests in Python: How Do I Get Started?
392
How to unit test abstract classes: extend with stubs?
373
Unit tests versus functional tests



All Articles
Loading...
X
Show
Funny
Dev
Pics