如何在Noje.js RESTful应用程序中进行集成testing?

鉴于现有的Node.js应用程序实现了JSON格式的RESTful API,在Node.js中编写一个集成testing套件有什么好的select?

该套件应执行testing场景,通常包括将数据库设置为已知状态(可能通过POST请求),并运行一系列涉及GET,POST,PUT和DELETE请求的testing,检查返回的状态码和响应。

unit testing框架有几个选项。

  • 誓言
  • 快报
  • nodeunit

我会用expresso和誓言来举例。

var assert = require('assert'), http = require('http'), server = getServer(); // get an instance of the HTTPServer assert.response(server, { 'url': '/' }, { 'body': "test body", 'status': "200" }); 

而且是一个誓言的例子

 var is = require("vows-is"); is.config({ server: { "factory": createServer, "kill": destroyServer, "uri": "http://localhost:4000" } }) is.suite("integration tests").batch() .context("a request to GET /") .topic.is.a.request("GET /") .vow.it.should.have.status(200) .vow.it.should.have .header("content-type", "text/html; charset=utf-8") .context("contains a body that") .topic.is.property('body') .vow.it.should.be.ok .vow.it.should.include.string("hello world") .suite().run({ reporter: is.reporter }, function() { is.end() }); 

誓言 – 是在誓言之上的一个稀薄的抽象使更加容易testing。

然而,誓言是在积极的发展,所以使用风险自负。