Unit Testing Nancy Routing, Controller and IoC<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Unit Testing Nancy Routing, Controller and IoC

Posted on: January 13, 2015

Maybe the title should be more integrating test with Nancy than unit testing because this is far more integrated than unit. However, the goal is to test and a NuGet package allow you to test easily test Nancy application. This NuGet package is called Nancy.Testing and can be installed with the following command.

1Install-Package Nancy.Testing

This allow to create a Browser (from the CsQuery.dll that is a dependency of the Nancy.Testing.dll) object that will act like a client to the Nancy server. From there, you create a unit test like usual and use the browser object to request to a specific url. Here is an example that request the base page (no page just the localhost). We expect to have a default webpage that return a 202 status.

2public class IndexModuleTest {
3 private readonly Browser browser;
4 public IndexModuleTest() {
5 var bootstrapper = new NancySelfHostedWithSignalR.Bootstrapper();
6 browser = new Browser(bootstrapper);
7 }
9 [TestMethod]
10 public void GivenDefaultUrl_WhenMarkdownListReturnData_ThenIndexPageLoad() {
11 var result = browser.Get("/", with => { with.HttpRequest();
12 });
14 Assert.AreEqual(HttpStatusCode.OK, result.StatusCode); }

One problem that you may stumble into is that the IoC of of BootStrapper is using some of the class that you may want to mock. This will require you to create a new Bootstrap and to override your default configuration. Instead of having multiple BootStrap class, we can have one for test where we can push all the mock required for each test. Imagine that you have a BootStrapper that define a lot of Interface to concrete type inside the ConfigureRequestContainer method. What you have to do it to override at your turn of your own Bootstrapper and override too the ConfigureRequestContainer. However, you call the base method before your testing ones. This way, it will override the one you redefine with your mock when testing.

1private class TestingBootstrapper : YourDefaultBootstrapper {
2 public List<Tuple<Type, object>> Types { get; set; }
4 public TestingBootstrapper(List<Tuple<Type, object>> types) { this.Types = types; }
6 protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context) {
7 base.ConfigureRequestContainer(container, context);
8 //Override here for test
9 foreach (var typeInterfaceAndImplementation in this.Types) { container.Register(typeInterfaceAndImplementation.Item1, typeInterfaceAndImplementation.Item2); }
10 }
13// Later inside one of your test:
14var types = new List<Tuple<Type, object>>();
15var bootstrapper = new TestingBootstrapper(types); var mock1 = new Mock<IFile1>();
17types.Add(new Tuple<Type, object>(typeof(IFile1),mock1.Object));
18var browser = new Browser(bootstrapper);

The browser uses your testing bootstrapper and in the same time redefine only the interface that you want for your test. This is interesting if you want to reuse a big part of your existing bootstrapper. Otherwise, you can just use the lambda action method to define your classes.

1var browser = new Browser((c) => c.Module<IndexModule>().Dependency<IFile1>(mock1.Object))