试驾 Nancy 模块
好的 - 我喜欢 NancyFx。用这么几行代码编写一个 Web 应用程序真是太棒了!
但是如何在单元级别上测试驱动 NancyModule?
请注意,我知道 Nancy 提供的优秀的测试框架(Nancy在 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()];
};
}
}
看到那里 - 现在我在测试之前编写了生产代码...:) 关于如何开始测试有什么建议吗?
Ok - I love NancyFx. Writing a web application with that few lines is just amazing!
But how do you test drive your NancyModules on the unit level?
Please note that I am aware of the excellent testframework supplied with Nancy (Nancy.Testing on NuGet), which gives excellent ways to test the whole (almost) application stack. But now I mean the unit level test I use to flesh out the contents of my NancyModule, in TDD fashion.
Since the routes are defined in the constructor, often together with a lamda expression that constitute the whole action, it feels a bit "unreachable" from a unit test. But have I missed something obvious on how to test the actions of the route?
For example, how would a unit test for this simple application look?
public class ResouceModule : NancyModule
{
private IProductRepository _productRepo;
public ResourceModule(IProductRepository repo) : base("/products")
{
Get["/list"] = parameters => {
return View["productList.cshtml", repo.GetAllProducts()];
};
}
}
See there - now I wrote the production code before the test... :) Any suggestions on how to start with the test?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用我们提供的测试工具进行测试第一个开发:
好的,所以您不只是测试模块,但是如果您查看调用堆栈,就会发现在您到达路线之前或之后没有发生太多事情,所以这对我来说并不是什么大不了的事情。书:-) 如果您确实想完全隔离地测试该模块,那么您可以自己构建它并相应地戳各个路由(它们只是模块中的字典)。
You can do test first dev with the testing tools we provide:
Ok, so you're not just testing the module, but if you look at the call stack, there's not much going on before or after you hit your route so it's not that big of a deal in my book :-) If you really do want to test the module in complete isolation then you can just construct it yourself and poke the individual routes accordingly (they're just dictionaries in the module).
作为 Nancy.Testing 的一部分,您可以使用可配置的引导程序来控制设置,包括 IoC 设置。这应该能够在没有较低级别依赖项的情况下测试模块,并启用 TDD。
As part of Nancy.Testing you can use the configurable bootsrapper to control the setup, including the IoC setup. That should enable testing the module without lower level dependencies, and enable TDD.