我的测试正在进行并且通过,但是chai的完成功能正在搞乱。我尝试了几种不同的方法,但我不明白在这些单元测试中我哪里出错了。我是单位测试和柴的新手,所以任何帮助都会非常感激
测试失败的地方:第40行(创建),第98行(更新)
有什么想法吗?
const chai = require('chai')
let should = chai.should()
let expect = chai.expect
let db = require('../app/models')
db.hosts.modelName = 'Hosts'
db.victims.modelName = 'Victims'
let models = [db.hosts, db.victims]
models.forEach(model => {
describe(`${model.modelName} Model`, function (done) {
var modelData = {
guest_count: 3,
start_date: "2018-01-11T00:00:00.000Z",
end_date: "2018-01-12T00:00:00.000Z",
location: {
type: "Point",
coordinates: [
-74.323564,
40.232323
]
},
first_name: "Sean",
last_name: "Destroyed",
phone: "7325556677",
address: "123 main street, red bank, nj",
email: "test@gmail.com",
}
it(`should create a new ${model.modelName}`, function () {
model.create(modelData).then(function (user) {
//victim name should be equivalent to the fake submission we are using
expect(user.first_name).to.equal("Sean");
//remove the entry from the database
model.destroy({
where: {
id: user.id
}
})
done()
})
});
it(`should delete a ${model.modelName} from the database`, function () {
model.create(modelData).then(function (user) {
//victim name should be equivalent to the fake submission we are using
//remove the entry from the database
model.destroy({
where: {
id: user.id
}
})
try {
model.findOne({
where: {
id: user.id
}
})
} catch (err) {
expect(user.first_name).to.undefined;
if (err) {
done()
}
}
})
})
it(`should update the ${model.modelName} entry in the database`, function () {
model.create(modelData).then(function (user) {
//after user is created, then update a value
modelData.guest_count = 12
model.update(modelData, {
where: {
id: user.id
}
}).then(function(data) {
model.findOne({
where: {
id: user.id
}
}).then(function (data) {
expect(data.guest_count).to.equal(12);
}).then(function () {
model.destroy({
where: {
id: user.id
}
})
}).then(function() {
done()
})
})
})
})
})
});
答案 0 :(得分:3)
要记住两件事:
(1)Sequelize使用其ORM方法的承诺。因此,即使在您呼叫destroy
之后,您也需要附加一个回调,例如:
model.destroy({
where: {
id: user.id
}
})
.then(function() {
// now do something
});
(2)chai中的done
方法应附加到每个测试中,而不是测试块:
describe('some test block', function() {
it('should do something,' function(done) {
User.findAll().then(function(users) {
// expect users to do something
done(); // tests are done
});
});
});
在您的情况下,这是两个失败的测试用例:
// ensure "destroy" has a callback
it(`should create a new ${model.modelName}`, function (done) {
model.create(modelData).then(function (user) {
//victim name should be equivalent to the fake submission we are using
expect(user.first_name).to.equal("Sean");
//remove the entry from the database
model.destroy({
where: {
id: user.id
}
}).then(function() {
done();
})
})
});
// update
it(`should update the ${model.modelName} entry in the database`, function () {
model.create(modelData).then(function (user) {
//after user is created, then update a value
modelData.guest_count = 12
model.update(modelData, {
where: {
id: user.id
}
}).then(function(data) {
model.findOne({
where: {
id: user.id
}
}).then(function (data) {
expect(data.guest_count).to.equal(12);
}).then(function () {
model.destroy({
where: {
id: user.id
}
}).then(function() {
done()
})
})
})
})
})
答案 1 :(得分:2)
@ mcranston18留下了一个非常好的详细接受答案。
我想为将来发现问题或OP的人添加的内容是使用async/await
:
describe('some test block', function() {
it('should do something', async function() { // notice async and no done
const users = await User.findAll()
// expect users.to (...)
})
})
这是使用async/await
创建然后更新的一种非常简单的方法:
describe('some test block', function () {
it('should do something', async function () {
const Joe = await User.create({ name: 'Jo' }) // oops
// assertions/expect/should
// ex: expect(Joe.name).to.equal('Jo')
await Joe.update({ name: 'Joe' }) // that's better
// assertions/expect/should
// ex: expect(Joe.name).to.equal('Joe')
})
})