Send parameter to Nancy module from test

て烟熏妆下的殇ゞ 提交于 2020-01-04 06:31:13

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/12996422/send-parameter-to-nancy-module-from-test

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!