Я запускаю следующий интеграционный тест для приложения Node.js. Первый API уже существует, в то время как вторая не делает:Mocha supertest интеграционный тест выходит слишком рано
'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][[email protected]:~/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, но я не могу сделать это неудачно, указав 200 с помощью .expect()
.
Почему тест не терпит неудачу? Обратите внимание, что getting here? 2
никогда не печатается.
Добавление 'done' в качестве аргумента функции и вызов ее в конце каждого теста решает проблему. Кажется, что Mocha ведет себя по-другому, если вы определяете функцию с 1 или 0 аргументами, я не знаю, как это делается, учитывая, как работает динамически типизированный JavaScript. – giorgiosironi