![](/img/trans.png)
[英]UnhandledPromiseRejectionWarning during testing with mocha-chai using chai-http
[英]Mocha before hook is not working with chai-http
這是我的測試代碼。 我正在測試一個API。 問題是“ after”掛鈎正在工作,並且在測試結束后刪除數據庫。 但是“之前”掛鈎不起作用。 這是什么問題? 我嘗試過但無法找出問題。 我試圖僅使用一個虛擬測試來運行before鈎子,例如在控制台中記錄一些內容。 也沒用。
const chai = require('chai'); const { assert } = require('chai'); const chaiHttp = require('chai-http'); const mongoose = require('mongoose'); mongoose.Promise = global.Promise; require('../resolvedir'); const User = require('models/User'); const server = require('bin/www'); const testData = require('./test_data'); chai.use(chaiHttp); describe('Empty User Collection before test', function () { it('should drop User Collection before test starts', function () { before(function (done) { User.collection.drop(); done(); }); }); }); describe('Testing /registration end point', () => { it('should return a valid JWT token', (done) => { chai.request(server) .post('/register') .send({ name: testData.name, email: testData.email, password: testData.password }) .end((err, res) => { assert.equal(res.status, 200, 'Http response code is 200'); assert.exists(res.body.auth, 'Auth confirmation message exist'); assert.isTrue(res.body.auth, 'Auth confirmation message is true'); assert.exists(res.body.token, 'JWT token is neither null or undefined'); assert.isString(res.body.token, 'JWT token is string'); done(); }); }); it('should fail registration', (done) => { chai.request(server) .post('/register') .send(testData) .end((err, res) => { assert.equal(res.status, 409, 'Http response code is 409'); assert.isString(res.body.message); assert.equal(res.body.message, 'User Exist'); done(); }); }); }); describe('Testing /login end point', function () { it('should get a valid JWT token on successful login', function (done) { chai.request(server) .post('/login') .send({ email: testData.email, password: testData.password }) .end((err, res) => { assert.isString(res.body.token, 'JWT token is string'); done(); }); }); }); describe('Empty User Collection after test', function () { it('should drop User Collection after test ends', function () { after(function (done) { User.collection.drop(); done(); }); }); });
Mongoose和MongoDB支持諾言。 chai-http
支持promise 。
before
應駐留在describe
塊內部,而不是it
。 Mocha支持對異步塊的承諾,無需done
任何done
。 但是測試使用的方式done
並且不一致。 before
是異步的,但done()
是同步調用的。 僅在測試成功時才調用done()
,當斷言失敗時,這會導致測試超時。
它應該是:
describe('Empty User Collection before test', function () {
before(function () {
return User.collection.drop();
});
it('should drop User Collection before test starts', function () {
...
});
});
和
it('should get a valid JWT token on successful login', function () {
return chai.request(server)
.post('/login')
.send({ email: testData.email, password: testData.password })
.then((res) => {
assert.isString(res.body.token, 'JWT token is string');
});
});
等等。
如果應該在“ Testing /registration end point
測試套件中刪除數據庫,則before
此before
也應該刪除數據庫-或所有測試都可以before
使用父類describe
:
describe('Suite', function () {
before(function () {
return User.collection.drop();
});
describe('Testing /registration end point', () => {...})
describe('Testing /login end point', () => {...})
...
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.