Pet*_*Put 2 unit-testing mocha.js node.js should.js supertest
我读过我可以使用超级测试在 Express 应用程序 (nodeJS) 中运行 mocha 测试,因此不需要在不同的终端会话中运行该应用程序。无论我尝试什么,它总是以连接错误结束。
要配置我们的持续集成,很明显集成和单元测试(mocha、supertest、should)应该能够在没有节点服务器也在运行的情况下运行
书面测试是为了验证我们应用程序的内部 api 端点 谁可以解释如何在不运行应用程序的 express 服务器的情况下运行测试,以便它们可以与例如 strider 集成
您需要拆分调用的生产代码,app.listen并确保在 mocha 测试运行期间不会执行该代码。我把我所有的路由和设置代码放进去app/index.js,然后有一个单独的文件app/server.js,其中只有一小部分代码可以开始监听、连接到数据库等。但是我的大部分应用程序细节都配置了,app/index.js所以我可以用超级测试。
//index.js
var app = require("express")();
app.get("/", function (req, res) {
res.type("text");
res.send("Welcome home");
});
module.exports = app;
Run Code Online (Sandbox Code Playgroud)
//server.js
#!/usr/bin/env node
var app = require("./index");
app.listen(3000);
Run Code Online (Sandbox Code Playgroud)
//index.mocha.js
var app = require("./index");
var request = require("supertest")(app);
var expect = require("expectacle");
describe("the home page", function () {
it("should welome me in plain text", function(done) {
request.get("/")
.expect(200)
.expect("Content-Type", "text/plain; charset=utf-8")
.end(function (error, result) {
expect(error).toBeFalsy();
expect(result.text).toBe("Welcome home");
done();
});
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2794 次 |
| 最近记录: |