试驾南希模块 [英] Test driving Nancy Modules

查看:54
本文介绍了试驾南希模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好-我爱NancyFx.用很少的几行编写一个Web应用程序真是太神奇了!

Ok - I love NancyFx. Writing a web application with that few lines is just amazing!

但是如何在单元级别上测试NancyModules?

But how do you test drive your NancyModules on the unit level?

请注意,我知道随附的出色的测试框架 Nancy(Nancy.Testing on NuGet),它提供了测试整个(几乎)应用程序堆栈的绝佳方法.但是,现在我的意思是我用来以TDD方式充实NancyModule内容的 unit 级别测试.

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.

由于路由是在构造函数中定义的,通常与构成整个操作的lamda表达式一起使用,因此从单元测试中感觉有些无法到达".但是我是否错过了一些显而易见的东西来测试路线的动作?

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?

推荐答案

您可以使用我们提供的测试工具进行首次开发测试:

You can do test first dev with the testing tools we provide:

  • 在测试启动时,配置一个引导程序,该引导程序仅包含要测试的模块以及所需的任何伪造对象.
  • 在测试中执行一条特定的路由(例如GET/list)-您可能需要一个小的帮助程序来删除一些重复的代码.
  • 声明返回的内容-您具有对请求和响应对象(用于标头,cookie等)的完全访问权限,以及用于HTML正文的帮助程序,并且在1.8中将提供用于处理JSON,XML和仅字符串响应的帮助程序身体.
  • 移至下一条路线,冲洗并重复.

好,所以您不是只是测试模块,但是如果您看一下调用堆栈,那么在到达路由之前或之后都不会进行太多操作,因此这并不是一个大问题在我的书中处理:-)如果您确实确实想完全隔离地测试模块,则可以自己构造它并相应地戳各个路由(它们只是模块中的字典).

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).

这篇关于试驾南希模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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