nunitmoqnancystructuremap3

Testing code in a custom NancyFx Bootstrapper


I have a custom Nancy Bootstrapper which uses StructureMapNancyBootstrapper but the issue is the same regardless of container.

public class CustomNancyBootstrapper : StructureMapNancyBootstrapper
{
    protected override void RequestStartup(IContainer container, IPipelines pipelines, NancyContext context)
    {
        var auth = container.GetInstance<ICustomAuth>();
        auth.Authenticate(context);
    }
}

I want to write a test to assert that Authenticate is called with the context... something like this...

[Test]
public void RequestStartup_Calls_CustomAuth_Authenticate_WithContext()
{
    // set up
    var mockAuthentication = new Mock<ICustomAuth>();
    var mockContainer = new Mock<IContainer>();
    var mockPipelines = new Mock<IPipelines>();
    var context = new NancyContext();

    mockContainer.Setup(x => x.GetInstance<ICustomAuth>()).Returns(mockAuthentication.Object);

    // exercise
    _bootstrapper.RequestStartup(_mockContainer.Object, _mockPipelines.Object, context);

    // verify
    mockAuthentication.Verify(x => x.Authenticate(context), Times.Once);
}

The problem is that I can't call RequestStartup because it's protected as defined in NancyBootstrapperBase.

protected virtual void RequestStartup(TContainer container, IPipelines pipelines, NancyContext context);  

Is there a "proper"/"offical" Nancy way to do this without creating another derived class and exposing the methods as that just seems like a hack?

Thanks


Solution

  • I guess you can "fake" the request by using Browser from Nancy.Testing:

     var browser = new Browser(new CustomNancyBootstrapper());
    
     var response = browser.Get("/whatever");
    

    There is a good set of articles about testing NancyFx application: http://www.marcusoft.net/2013/01/NancyTesting1.html