Question

I've set up a Web API project using Ninject, and I've used the fix detailed here for getting it to work with the latest version of the Web API. Everything is working fine, but I'm now trying to write some tests.

I'm using in-memory hosting to run the project for the tests, as detailed here, as I have a DelegatingHandler that performs authentication and then sets a property on the request message that is used by all the Api Controllers.

So, I've got a base class for my tests, and have a SetUp method where I set up the HttpServer and configuration, which I've pretty much taken from my working Ninject code:

[SetUp]
public void Setup()
{
    bootstrapper = new Bootstrapper();

    DynamicModuleUtility.RegisterModule(
        typeof(OnePerRequestHttpModule));

    DynamicModuleUtility.RegisterModule(
        typeof(NinjectHttpModule));

    bootstrapper.Initialize(CreateKernel);

    var config = new HttpConfiguration();
    config.Routes.MapHttpRoute("Login",
        "api/auth/token",
        new { controller = "Users", action = "Login" });

    config.IncludeErrorDetailPolicy =
        IncludeErrorDetailPolicy.Always;

    config.DependencyResolver = 
        new NinjectResolver(CreateKernel());

    config.MessageHandlers.Add(
        new AuthenticationHandler(CreateUserManager()));

    Server = new HttpServer(config);
}

This is how I create the MoqMockingKernel:

private static IKernel CreateKernel()
{
    var kernel = new MoqMockingKernel();
    kernel.Bind<Func<IKernel>>()
        .ToMethod(ctx => () => new Bootstrapper().Kernel);
    kernel.Bind<IHttpModule>()
        .To<HttpApplicationInitializationHttpModule>();

    RegisterServices(kernel);

    GlobalConfiguration.Configuration.DependencyResolver = 
        new NinjectResolver(kernel);

    return kernel;
}

And this is how I register the objects to use:

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IUserManager>().ToMock();

    kernel.Bind<UsersController>().ToSelf();
}

While I'm not testing the Controller per se, I do want a proper instance of it to be called, which is why I'm binding it ToSelf. I must admit that I am assuming that this is correct. This is an example of a test:

public void UserCannotLogin()
{
    System.Net.Http.HttpClient client = 
        new System.Net.Http.HttpClient(Server);

    string json = string.Format(
        "{{ \"Username\": \"{0}\", \"Password\": \"{1}\" }}", 
        "wrong", "wrong");

    HttpRequestMessage request = 
        CreateRequest(@"api/auth/token", json, HttpMethod.Get);

    Action action = () => client.SendAsync(request);

    using (var response = client.SendAsync(request).Result)
    {
        response.StatusCode.Should()
            .Be(HttpStatusCode.Unauthorized);
    }
}

I'm basically getting a 404 error. When I debug it, it does go to my DelegatingHandler, but it doesn't go to my controller.

I get the feeling that I'm fundamentally missing a point here, and it may not even be possible to do what I'm trying to do, but if anyone has any suggestions for either how to do this, or a different way to achieve the same thing, I'm all ears.

Update I think that it's because the default behaviour of the MockingKernel is to provide a Mock unless told otherwise, so it is returning a Mock of IHttpControllerSelector. I've set up a couple of default ones now:

kernel.Bind<IHttpControllerSelector>()
    .To<DefaultHttpControllerSelector>();
kernel.Bind<IContentNegotiator>()
    .To<DefaultContentNegotiator>();

It's still not working, I think because there are no formatters specified. I'll try that tomorrow and see if that gets me there.

Was it helpful?

Solution

Ok, I think that I was correct when I said that I was fundamentally missing a point here, but I'll answer this in case it helps someone else avoid the same mistake!

The Ninject MockingKernel is, I think, primarily about auto-mocking, so where you have a lot of interfaces you don't care about how they are set up in your test, you can ignore them in your tests and they will be automatically created for you.

In the case of the Web API, this is most definitely not the case, as you don't want the controller selector class to be auto mocked, otherwise you won't end up calling your controllers.

So, the solution I've come up with is to stick with using a standard Ninject Kernel, and then bind your interface to a constant Mock object:

kernel.Bind<IUserManager>().ToConstant(CreateUserManager());

private IUserManager CreateUserManager()
{
    Mock<IUserManager> userManager = new Mock<IUserManager>();

    // Set up the methods you want mocked

    return userManager.Object;
}

Doing this, I've been able to successfully write tests that use an HttpClient to call an in-memory HttpServer that successfully call my DelegatingHandler and then end up at my controllers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top