Search code examples
testingnancy

Send parameter to Nancy module from test


I'm trying to test a module with parameter (below is only code where I'm trying to figure the problem out)

public class StuffModule : NancyModule
{
    public StuffModule() : base("/Stuff")
    {
        Get["/All/"] = parameters =>
                       {
                           string str = parameters.one;
                           return Response.AsJson(str);
                       };
    }
}

private Browser _browser;

[SetUp]
public void SetUp()
{
    var module = new StuffModule(null);

    var mock = new Mock<IRecipeExtractor>();
    var bootstrapper = new ConfigurableBootstrapper(
            with => with.Dependency(mock.Object)
        );

    _browser = new Browser(bootstrapper);
}

[Test]
public void Can_extract_recipe_as_json()
{
    var result = _browser.Get("/Stuff/All/", with =>
    {
        with.HttpRequest();
        with.Query("one", "yes_one");
    });

    Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK));
}

When running above code I get nothing in my parameters variable. Some hints?


Solution

  • In order to capture parameters you need to declare them as part of your part such as /profile/{username} which would result in parameters.username to be accessible.

    You are passing in a querystring value into your test, which is accessible on Request.Query.one and you can ensure it has a value with Request.Query.one.HasValue

    You can read a bit more about it here https://github.com/NancyFx/Nancy/wiki/Defining-routes and here https://github.com/NancyFx/Nancy/wiki/Testing-your-application