好的 - 我喜欢NancyFx.使用这几行编写Web应用程序真是太棒了!
但是,如何在单元级别上测试驱动NancyModules?
请注意,我知道Nancy提供的出色的测试框架(Nancy.Testing on NuGet),它提供了测试整个(几乎)应用程序堆栈的出色方法.但现在我指的是我用来以TDD方式充实我的NancyModule内容的单元级别测试.
由于路由是在构造函数中定义的,通常与构成整个动作的lamda表达式一起,因此单元测试感觉有点"无法访问".但是,我是否错过了关于如何测试路线行为的明显错误?
例如,如何对这个简单的应用程序进行单元测试?
public class ResouceModule : NancyModule
{
private IProductRepository _productRepo;
public ResourceModule(IProductRepository repo) : base("/products")
{
Get["/list"] = parameters => {
return View["productList.cshtml", repo.GetAllProducts()];
};
}
}
Run Code Online (Sandbox Code Playgroud)
看到那里 - 现在我在测试之前编写了生产代码...... :)关于如何开始测试的任何建议?
我有一个简单的南希模块.我想将查询字符串(qs)参数传递给处理程序.如果我没有任何qs参数,一切都很好.一旦我添加了一个参数,我就会收到一个404状态代码.
NancyModule
public class SimpleModule : NancyModule
{
public SimpleModule()
{
Get["/"] = parameters => HttpStatusCode.OK;
}
}
Run Code Online (Sandbox Code Playgroud)
单元测试 - 通行证
[Fact]
public void SimpleModule__Should_return_statusOK_when_passing_query_params()
{
const string uri = "/";
var response = Fake.Browser().Get(uri, with => with.HttpRequest());
response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Run Code Online (Sandbox Code Playgroud)
单元测试 - 失败
[Fact]
public void SimpleModule__Should_return_statusOK_when_passing_query_params()
{
const string uri = "/?id=1";
var response = Fake.Browser().Get(uri, with => with.HttpRequest());
response.StatusCode.ShouldBe(HttpStatusCode.OK);
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在尝试访问该CurrentUser属性NancyContext.如何在Razor视图的html中执行此操作?
如果可能的话,我会很感激代码片段.
谢谢
编辑
我现在扩展Nancy.ViewEngines.Razor.HtmlHelpers为给我带有语法糖的交叉视图数据,使视图代码简洁易读.
这里有一些例子:
public static bool IsRegistered<T>(this HtmlHelpers<T> html)
{
var user = GetUser(html);
return user != null && user.IsRegistered;
}
public static bool IsAuthenticated<T>(this HtmlHelpers<T> html)
{
return GetUser(html) != null;
}
public static User GetUser<T>(this HtmlHelpers<T> html)
{
return (User)html.RenderContext.Context.CurrentUser;
}
Run Code Online (Sandbox Code Playgroud)
还有一些剃刀代码.这里我决定只在用户当前未经过身份验证的情况下包含用于登录弹出窗口(基础显示)的html - 这是有意义的.
@if (!Html.IsAuthenticated())
{
Html.Partial("Reveals/SignInReveal");
}
Run Code Online (Sandbox Code Playgroud) 在ASP.NET中,每当我在VS2012中以调试模式运行服务器时,我对静态内容(js,css等)所做的任何更改都会在保存后立即反映出来.
在NancyFX中,我每次更改静态内容时都需要重新启动服务器.我假设这是因为VS每次运行服务器时都需要将静态内容复制到输出目录.
无论如何反映保存后立即对静态内容所做的更改?
这是我对静态内容的配置
public class MainBootstrapper : DefaultNancyBootstrapper
{
protected override void ConfigureConventions(NancyConventions nancyConventions)
{
nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("Scripts"));
base.ConfigureConventions(nancyConventions);
}
}
Run Code Online (Sandbox Code Playgroud)
这也许是相关的.我在一个控制台应用程序下运行它,其中nancyfx主循环编写如下:
class Program
{
const ushort port = 64402;
const string escapeString = "/Terminate";
static void Main(string[] args)
{
NancyHost host;
#region Making new instance of NancyHost
var uri = new Uri("http://localhost:" + port + "/");
var config = new HostConfiguration(); config.UrlReservations.CreateAutomatically = true;
host = new NancyHost(config, uri);
#endregion
#region NancyFX hosting loop
try
{
host.Start();
Console.Write("Start hosting the …Run Code Online (Sandbox Code Playgroud) 目前我在南希使用剃须刀作为我的视图引擎.
我可以在剃刀中访问我的资源文件:
@Text.text.greeting
Run Code Online (Sandbox Code Playgroud)
但我想切换到另一个视图引擎.
是否有其他可用的视图引擎支持TextResource?
本地化如何在超级简单视图引擎中运行?
或者有没有办法使用模型访问资源?
有没有办法为Nancy服务自动生成Swagger文档(或类似文档)?
我找到了Nancy.Swagger,但是没有关于如何使用它的信息,并且演示应用程序似乎没有演示生成文档(如果有的话,它并不明显).
任何帮助,将不胜感激.谢谢!
我是否需要在应用程序Bootstrapper中添加任何配置代码以在Nancy中启用FluentValidation?
按照https://github.com/NancyFx/Nancy/tree/master/src/Nancy.Demo.Validation中的示例,我尝试在模型上使用this.Validate时收到以下异常消息:无法找到模型验证工厂.
我正在使用Nancy版本0.11.0.0
我希望我的静态内容(图像,javascript文件,css文件等)只有在文件更新后才能完整提供.
如果自上次请求的文件并没有改变(如由确定ETag和Last-Modified响应报头值),那么我想通过客户端浏览器中使用的文件的缓存版本.
南希是否支持此功能?
我需要在Nancy中显示我的404错误页面
if (ErrorCode == 404)
{
return View["404.html"];
}
Run Code Online (Sandbox Code Playgroud)
怎么做?
我尝试在URL上创建一个带NancyModules和GET字符串的类,但方法'Get'告诉我:
"错误CS0021无法将[]索引应用于"方法组"类型的表达式...."
我的代码:
using System; using System.Collections.Generic; using System.Linq;
using System.Web; using Nancy; using System.Text;
namespace SinglePageApp1 {
public class BloodPresureNancy : NancyModule
{
public BloodPresureNancy()
{
// Get dasn't work
Get["/"] = _ => "Heloooo";
}
}
}
Run Code Online (Sandbox Code Playgroud)
我添加了引用:Nancy,Nancy.Hosting.asp,它无法正常工作.
nancy ×10
.net ×3
c# ×3
asp.net ×2
razor ×2
get ×1
linq ×1
query-string ×1
swagger ×1
unit-testing ×1