2011-08-30 109 views
4

鉴于实现一个RESTful API和JSON格式的现有Node.js应用程式,这将是写一个集成测试套件Node.js的好办法吗?如何在Noje.js RESTful应用程序中进行集成测试?

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

+0

我发现了一个类似的问题:http://stackoverflow.com/questions/7127226/node-js-testing-restful-api-vows-js –

回答

4

有单元测试框架的几个选项。

我将展示与快报&誓言例子。

var assert = require('assert'), 
    http = require('http'), 
    server = getServer(); // get an instance of the HTTPServer 

assert.response(server, { 
    'url': '/' 
}, { 
    'body': "test body", 
    'status': "200" 
}); 

并与vows-is

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() 
}); 

一个例子的愿-IS是上的愿的顶部上的薄的抽象,使其更容易与测试。

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

+0

我能够实现[集成测试](https:// github.com/fernandoacorreia/pintura-blog/blob/master/test/test.js)与誓言,是和它的工作好。谢谢。 –

+1

别忘了茉莉花/茉莉花节! –

+0

链接到誓言是(https://github.com/Raynos/vows-is)坏了,找不到回购。 – bithavoc

相关问题