将参数从测试发送到Nancy模块 [英] Send parameter to Nancy module from test

查看:143
本文介绍了将参数从测试发送到Nancy模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用参数测试模块(下面是我试图解决问题的唯一代码)

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));
}

在上面的代码中运行时,我的参数变量什么也没得到.一些提示吗?

解决方案

为了捕获参数,您需要将它们声明为零件的一部分,例如/profile/{username},这将导致parameters.username可以访问.

您正在将一个查询字符串值传递到您的测试中,该值可以在Request.Query.one上访问,并且可以使用Request.Query.one.HasValue

确保它具有一个值.

您可以在此处了解更多相关信息 https://github.com/NancyFx /Nancy/wiki/定义路线,然后在此处 https://github .com/NancyFx/Nancy/wiki/Testing-your-application

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?

解决方案

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

这篇关于将参数从测试发送到Nancy模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆