如果我进行单元测试,
test('it saves included instances', async() => {
/* Get "water" composition */
const compositions = await _compositions.findAll(
{include: [{model: _elements, where: {name: 'water'}}, _atoms]});
/**
* compositions[0] -> Hydrogren
* compositions[1] -> Oxygen
*/
let hydrogen = compositions[0].get('atom');
/* rename "Hydrogen" to "My super Hydrogen" */
hydrogen.name = 'My super Hydrogen';
/* save the composition */
compositions[0].save();
/* Get a fresh Hydrogen Instance from the database */
hydrogen = await _atoms.find({where: {symbol: 'H'}});
expect(hydrogen.name)
.to.equal('My super Hydrogen', 'wrong, the value is still Hydrogen');
});
我希望该值等于先前从相关对象保存的值。
atom
的属性composition
是外键:
compositionModel.belongsTo(await getAtomModel(), {foreignKey: 'atom_id'});
答案 0 :(得分:0)
在Sequelize中更新对象时,应该在实例本身上调用.update
test('it saves included instances', async() => {
const compositions = await _compositions.findAll(
{include: [{model: _elements, where: {name: 'water'}}, _atoms]});
let hydrogen = compositions[0].get('atom');
let newName = 'My super Hydrogen';
compositions[0].update({
name: newName
});
/* Get a fresh Hydrogen Instance from the database */
hydrogen = await _atoms.find({where: {symbol: 'H'}});
expect(hydrogen.name)
.to.equal(newName);
});