将参数从测试发送到Nancy模块 [英] Send parameter to Nancy module from test
问题描述
我正在尝试使用参数测试模块(下面是我试图解决问题的唯一代码)
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屋!