sequelize 不插入数据库,(sequelize,nodejs,mocha,chai)
sequelize not inserting to database, (sequelize, nodejs, mocha, chai)
我正在使用 sequelize ORM 和 node js 并尝试在数据库中创建一个新记录 在使用 mocha 和 chai 编写的测试用例中,执行的测试用例下面是代码和输出
但是没有插入数据库
我期待数据库中有一条新记录,但它没有插入
const app = require('../../app');
const chai = require('chai');
const should = chai.should();
const expect = chai.expect();
const models = require('../../app/models/index');
const User = models.User;
let assert = chai.assert;
describe('Model:User - attribute: password_digest - unit tests', () => {
it('password_digest should not be empty', (done) => {
console.log('>>>1');
console.log(User.create);
User.create({
email: 'aa@gmail.com',
password: 'aa',
password_confirmation: 'aa',
role_id: 'User',
profile_id: 'Consultant'
}).then(function(user) {
console.log('>>>2');
console.log(user);
});
console.log('>>>3');
done();
});
});
Model:User - attribute: password_digest - unit tests
test_1 | >>>1
test_1 | [Function]
test_1 | >>>3
test_1 | Γ£ô password_digest should not be empty
你的承诺大概是失败了。您需要检查您的错误日志以找出发生了什么,或者处理您的承诺的失败案例,例如:
describe('Model:User - attribute: password_digest - unit tests', () => {
it('password_digest should not be empty', (done) => {
console.log('>>>1');
console.log(User.create);
User.create({
email: 'aa@gmail.com',
password: 'aa',
password_confirmation: 'aa',
role_id: 'User',
profile_id: 'Consultant'
}).then(function(user) {
console.log('>>>2');
console.log(user);
}).catch(function(err) {
// Ideally, every time you handle the success of a promise
// with `then`, you should also handle the possible failure
// of it with `catch`
console.log('Error inserting user:');
console.log(err);
});
console.log('>>>3');
done();
});
});
然后,使用具体的错误信息更新您的问题,我们可以进一步帮助您。
你需要在你的两个 promise 处理程序中调用 done()
,并且只有在:已完成的分支(then
),所以你在告诉 mocha 测试完成之前等待 Promise 解决,并捕获任何错误(在 catch
分支中)。所以你的代码可以是:
it('password_digest should not be empty', (done) => {
User.create({
email: 'aa@gmail.com',
password: 'aa',
password_confirmation: 'aa',
role_id: 'User',
profile_id: 'Consultant'
})
.then(function(user) {
// your user assertions
console.log(user);
done();
}).catch(done);
});
您当前的测试版本在调用 User.create
之后立即调用 done()
,因此您没有给数据库执行插入的时间。
同时使用 done
作为您的 catch
处理程序可确保承诺链中的任何错误都不会阻止您的测试完成,并且可以为您打印错误。使用任何值调用 done()
会将测试标记为失败。
我正在使用 sequelize ORM 和 node js 并尝试在数据库中创建一个新记录 在使用 mocha 和 chai 编写的测试用例中,执行的测试用例下面是代码和输出
但是没有插入数据库 我期待数据库中有一条新记录,但它没有插入
const app = require('../../app');
const chai = require('chai');
const should = chai.should();
const expect = chai.expect();
const models = require('../../app/models/index');
const User = models.User;
let assert = chai.assert;
describe('Model:User - attribute: password_digest - unit tests', () => {
it('password_digest should not be empty', (done) => {
console.log('>>>1');
console.log(User.create);
User.create({
email: 'aa@gmail.com',
password: 'aa',
password_confirmation: 'aa',
role_id: 'User',
profile_id: 'Consultant'
}).then(function(user) {
console.log('>>>2');
console.log(user);
});
console.log('>>>3');
done();
});
});
Model:User - attribute: password_digest - unit tests
test_1 | >>>1
test_1 | [Function]
test_1 | >>>3
test_1 | Γ£ô password_digest should not be empty
你的承诺大概是失败了。您需要检查您的错误日志以找出发生了什么,或者处理您的承诺的失败案例,例如:
describe('Model:User - attribute: password_digest - unit tests', () => {
it('password_digest should not be empty', (done) => {
console.log('>>>1');
console.log(User.create);
User.create({
email: 'aa@gmail.com',
password: 'aa',
password_confirmation: 'aa',
role_id: 'User',
profile_id: 'Consultant'
}).then(function(user) {
console.log('>>>2');
console.log(user);
}).catch(function(err) {
// Ideally, every time you handle the success of a promise
// with `then`, you should also handle the possible failure
// of it with `catch`
console.log('Error inserting user:');
console.log(err);
});
console.log('>>>3');
done();
});
});
然后,使用具体的错误信息更新您的问题,我们可以进一步帮助您。
你需要在你的两个 promise 处理程序中调用 done()
,并且只有在:已完成的分支(then
),所以你在告诉 mocha 测试完成之前等待 Promise 解决,并捕获任何错误(在 catch
分支中)。所以你的代码可以是:
it('password_digest should not be empty', (done) => {
User.create({
email: 'aa@gmail.com',
password: 'aa',
password_confirmation: 'aa',
role_id: 'User',
profile_id: 'Consultant'
})
.then(function(user) {
// your user assertions
console.log(user);
done();
}).catch(done);
});
您当前的测试版本在调用 User.create
之后立即调用 done()
,因此您没有给数据库执行插入的时间。
同时使用 done
作为您的 catch
处理程序可确保承诺链中的任何错误都不会阻止您的测试完成,并且可以为您打印错误。使用任何值调用 done()
会将测试标记为失败。