【问题标题】:How to do integration testing in a Noje.js RESTful application?如何在 Node.js RESTful 应用程序中进行集成测试?
【发布时间】:2011-08-30 19:08:02
【问题描述】:

鉴于现有的 Node.js 应用程序实现了 JSON 格式的 RESTful API,在 Node.js 中编写集成测试套件有哪些好的选择?

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

【问题讨论】:

标签: node.js integration-testing


【解决方案1】:

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

我将展示带有表达和誓言的示例。

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

vows-is 是 vows 之上的一个精简抽象,使其更易于测试。

但是 vows-is 正在积极开发中,因此使用风险自负。

【讨论】:

  • 我能够使用 vows-is 实现 integration test 并且运行良好。谢谢。
  • 别忘了 Jasmine/Jasmine-node !
  • 与 vows-is(github.com/Raynos/vows-is) 的链接已损坏,找不到 repo。
  • vows-is 是一个错误,已被删除。
猜你喜欢
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-02
  • 2019-01-22
相关资源
最近更新 更多