Contents

Nancy and NCrunch - Playing nicely together...at last

If you haven’t tried NCrunch before then stop reading this and go download the trial and try it out, it will forever change your testing habits* - for the better.

NCrunch is the breakfast of champions

-KenR

At DrDoctor we use Nancy (rather than the typically used ASP.Net MVC Framework) the main reason is summed up by the overall goal of the framework:

The goal of the framework is to stay out of the way as much as possible and provide a super-duper-happy-path to all interactions.

One of the greatest things about Nancy vs ASP.Net MVC is that everything can be tested, and we have tests for pretty much all of our Modules (read Controllers) as well as our Views. NCrunch does some serious magic when it comes to running your test intelligently in parallel. Up until a few days ago I couldn’t figure out how to get my tests to work.

Testing Nancy Views

Testing Nancy Views is very easy, for demonstration purposes I created a new Nancy project from the standard ASP.Net and Razor Nancy template.

/nancy-and-ncrunch-playing-nicely-together-at-last/images/6-sample-solution.png

The template creates a module called IndexModule which returns a view called Index.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
namespace TestingDemo
{
  using Nancy;
 
  public class IndexModule : NancyModule
  {
    public IndexModule()
    {
      Get["/"] = parameters =>
      {
        return View["index"];
      };
    }
  }
}

I then created an MS Test project called specs, and added the following test to make sure that when someone browses to “/” they would be served up the Index view

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public void Should_Return_IndexView()
{
 //arrange
  var bootstrapper = new ConfigurableBootstrapper(with =>
  {
    with.ViewFactory<TestingViewFactory>();
    with.RootPathProvider<TestRootPathProvider>();
    with.Module<IndexModule>();
  });
 
  var browser = new Browser(bootstrapper);
 
  //act
  var response = browser.Get("/");
 
  //assert
  Assert.AreEqual("index", response.GetViewName());
}

To make this work I needed to implement my own custom IRootPathProvider,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class TestRootPathProvider : IRootPathProvider
{
  public string GetRootPath()
  {
    string assemblyFilePath = new Uri(typeof(TestingDemo.IndexModule).Assembly.CodeBase).LocalPath;
    var assemblyPath = System.IO.Path.GetDirectoryName(assemblyFilePath);
    var path = PathHelper.GetParent(assemblyPath, 3);
    path = System.IO.Path.Combine(path, "TestingDemo");
 
    return path;
  }
}

In the Specs project I reference the TestingDemo project and then added the Nancy.Testing and Nancy Razor View Engine (since I’m testing Razor views) NuGet packages.

At this point the test passes in the visual studio test runner, but fails in NCrunch.

Eating Nancy tests with NCrunch

To get the tests to pass in NCrunch you will need to make a few changes to the NCrunch configuration for the test project.

  1. Open the NCrunch configuration window from the NCrunch menu and select the test project (below mine is called Specs)

  2. Under General select “Additional files to include”

    /nancy-and-ncrunch-playing-nicely-together-at-last/images/1-ncrunch-config1.png

  3. Click the button with three dots (see highlighted in red above), on the Additional Files to Include dialog click “Add Directory”

  4. Browse to and select the root folder of your Nancy project, then click OK

  5. You will see the folder appear in the Additional Files To Include window, click OK

    /nancy-and-ncrunch-playing-nicely-together-at-last/images/2-additional-files-to-include.png

  6. Back in the NCrunch configuration window, under Build Settings set “Copy referenced assemblies to workspace” to True

    /nancy-and-ncrunch-playing-nicely-together-at-last/images/4-copy-references-assemblies.png

  7. You can now close the NCrunch configuration window

  8. You should see that the test has now passed in the NCrunch Tests window

    /nancy-and-ncrunch-playing-nicely-together-at-last/images/5-tests-have-passed.png

*May not actually change your testing habits.

🍪 I use Disqus for comments

Because Disqus requires cookies this site doesn't automatically load comments.

I don't mind about cookies - Show me the comments from now on (and set a cookie to remember my preference)