【发布时间】:2020-04-18 18:42:55
【问题描述】:
我有以下测试
describe('User - Sign up', function() {
it('should not register a new user because of missing data on form', function (done) {
const email = "testemail@gmail.com";
const req = { body: { email: email }};
const signUp = controller.signUp(req);
console.log(signUp);
signUp
.then(data => {
console.log('ok');
})
.catch(err => {
console.log('error')
});
done();
});
注册功能如下:
return new Promise(((resolve, reject) => {
validator.checkAsync(async () => {
const user = {
email: req.body.email,
password: await pwdHash.hashPassword(req.body.password)
}
await store.signUp(user);
resolve({
valid: true
});
}, () => {
reject(validator.errors);
});
}));
但是,测试没有返回任何控制台日志并且它正在通过。 我错过了什么?
更新:
我可以使用以下代码修复它:
it('should not register a new user when missing data on form', async function () {
const email = "testemail@gmail.com";
const res = await chai.request('http://localhost:3000')
.post('/user/signup',)
.send({ email: email });
expect(res).to.have.status(401);
});
基于 cmets 和此评论: https://stackoverflow.com/a/46217308/868699
【问题讨论】:
标签: node.js unit-testing express testing