Mocha supertest 集成测试退出太早
Mocha supertest integration test exits too soon
我正在 运行 对 Node.js 应用程序进行以下集成测试。第一个 API 已经存在,而第二个不存在:
'use strict';
var app = require('../..');
import request from 'supertest';
describe('Planes API:', function() {
describe('GET /api/planes/:name', function() {
it('should contain a list of alive cells', function() {
request(app)
.get('/api/planes/a-block-and-bar')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
console.log("getting here? 1");
var plane = res.body;
plane.aliveCells.length.should.equal(3);
});
});
it('should load a specific generation', function() {
request(app)
.get('/api/planes/a-block-and-bar/generation/1')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
console.log("getting here? 2");
res.status.should.equal(200);
});
});
});
});
然而,输出是:
[11:39:15][giorgio@Bipbip:~/code/game-of-life-javascript]$ grunt mochaTest:integration
Running "mochaTest:integration" (mochaTest) task
Planes API:
Express server listening on 9000, in development mode
GET /api/planes/:name
✓ should contain a list of alive cells
GET /api/planes/a-block-and-bar 200 6.082 ms - 58
getting here? 1
✓ should load a specific generation
2 passing (94ms)
GET /api/planes/a-block-and-bar/generation/1 404 4.258 ms - -
Done, without errors.
Execution Time (2016-02-07 10:42:13 UTC)
loading tasks 220ms ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 14%
loading grunt-mocha-test 46ms ▇▇▇▇ 3%
mochaTest:integration 1.3s ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 83%
Total 1.5s
所以测试给出了假阴性。输出显示 404 由 API 返回,但我无法通过使用 .expect()
.
指定 200 使其失败
为什么测试没有失败?注意 getting here? 2
永远不会被打印出来。
我会试试
it('should load a specific generation', function(done) {
然后在 end
方法中调用 done()
。如果您的第二个日志从未打印过,则此异步测试应该会产生超时错误。我不熟悉超级测试,但它似乎不会在 404
的情况下触发。可能存在错误事件处理程序?
我正在 运行 对 Node.js 应用程序进行以下集成测试。第一个 API 已经存在,而第二个不存在:
'use strict';
var app = require('../..');
import request from 'supertest';
describe('Planes API:', function() {
describe('GET /api/planes/:name', function() {
it('should contain a list of alive cells', function() {
request(app)
.get('/api/planes/a-block-and-bar')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
console.log("getting here? 1");
var plane = res.body;
plane.aliveCells.length.should.equal(3);
});
});
it('should load a specific generation', function() {
request(app)
.get('/api/planes/a-block-and-bar/generation/1')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
console.log("getting here? 2");
res.status.should.equal(200);
});
});
});
});
然而,输出是:
[11:39:15][giorgio@Bipbip:~/code/game-of-life-javascript]$ grunt mochaTest:integration
Running "mochaTest:integration" (mochaTest) task
Planes API:
Express server listening on 9000, in development mode
GET /api/planes/:name
✓ should contain a list of alive cells
GET /api/planes/a-block-and-bar 200 6.082 ms - 58
getting here? 1
✓ should load a specific generation
2 passing (94ms)
GET /api/planes/a-block-and-bar/generation/1 404 4.258 ms - -
Done, without errors.
Execution Time (2016-02-07 10:42:13 UTC)
loading tasks 220ms ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 14%
loading grunt-mocha-test 46ms ▇▇▇▇ 3%
mochaTest:integration 1.3s ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 83%
Total 1.5s
所以测试给出了假阴性。输出显示 404 由 API 返回,但我无法通过使用 .expect()
.
为什么测试没有失败?注意 getting here? 2
永远不会被打印出来。
我会试试
it('should load a specific generation', function(done) {
然后在 end
方法中调用 done()
。如果您的第二个日志从未打印过,则此异步测试应该会产生超时错误。我不熟悉超级测试,但它似乎不会在 404
的情况下触发。可能存在错误事件处理程序?