【发布时间】:2021-12-04 14:11:41
【问题描述】:
我是 sinon, chai and mocha 测试的新手
我正在使用这三个测试库和包sequelize-test-helpers
我正在尝试运行测试以检查 belongsTo 关联是否正常工作,这是我采取的步骤:
首先是我使用 sequelize 的数据库中的实际表,数据库是 Postgres
module.exports = (sequelize, DataTypes) => {
// const { DataTypes } = Sequelize;
const DealerProduct = sequelize.define('DealerProduct', {
dpid: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true
},
product_name: {
type: DataTypes.STRING(100),
allowNull: false,
unique: true,
// validate: {
// len: [1, 100]
// }
},
type: {
type: DataTypes.STRING(25),
allowNull: true,
// validate: {
// len: [1, 100]
// }
},
description: {
type: DataTypes.TEXT,
allowNull: true
},
price: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
quantity: {
type: DataTypes.INTEGER,
allowNull: false
}
}, {
tableName: 'dealer_products',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
// freezeTableName: true
// paranoid: true
});
DealerProduct.associate = models => { // **For starters this is the belongsTo association I would like to make my test for**
DealerProduct.belongsTo(models.Dealer, {
foreignKey: {
type: DataTypes.UUID,
allowNull: false,
name: "dealers_did",
}
});
DealerProduct.hasOne(models.StoreProduct, {
foreignKey: {
type: DataTypes.UUID,
allowNull: false,
name: 'dealer_product_dpid'
}
})
};
return DealerProduct;
}
这是测试
const {
sequelize,
dataTypes,
checkModelName,
checkPropertyExists,
checkUniqueIndex
} = require('sequelize-test-helpers');
const chai = require("chai");
const sinon = require("sinon");
const sinonChai = require("sinon-chai");
chai.should();
chai.use(sinonChai)
const DealerProductModel = require('../../models/dealer-products-model');
describe('server/models/all', async () => {
describe('server/models/dealer-products-model', async () => {
const DealerProduct = DealerProductModel(sequelize, dataTypes);
const dealerProduct = new DealerProduct();
checkModelName(DealerProduct)('DealerProduct');
describe('check all properties exist', () => {
['dpid', 'product_name', 'type', 'description', 'price', 'quantity'].forEach(checkPropertyExists(dealerProduct))
})
describe('check associations', () => {
const OtherModel = 'Dealer' // it doesn't matter what
before(() => {
DealerProduct.associate( { OtherModel } )
})
it('defined a belongsTo association with Dealer', () => {
chai.expect(DealerProduct.belongsTo).to.have.been.calledWith(OtherModel, {
foreignKey: {
type: dataTypes.UUID,
allowNull: false,
name: 'dealer_product_dpid'
}
})
})
})
})
})
我是从 page 开始关注这个例子的
我不知道如何将 sinon 与 chai 链接,以便 calledWith 可以工作,但我四处搜索发现在导入它们时会这样做(可能我在这里错了或遗漏了一些东西):
const chai = require("chai");
const sinon = require("sinon");
const sinonChai = require("sinon-chai");
chai.should();
chai.use(sinonChai)
它可以让calledWith 工作,因为我正在窥探通过这个expect 方法查看关联:
describe('check associations', () => {
const OtherModel = 'Dealer' // it doesn't matter what
before(() => {
DealerProduct.associate( { OtherModel } )
})
it('defined a belongsTo association with Dealer', () => {
chai.expect(DealerProduct.belongsTo).to.have.been.calledWith(OtherModel, {
foreignKey: {
type: dataTypes.UUID,
allowNull: false,
name: 'dealer_product_dpid'
}
})
})
})
我收到此错误:
AssertionError: expected belongsTo to have been called with arguments 'Dealer', {
foreignKey: {
type: [Function: Noop],
allowNull: false,
name: 'dealer_product_dpid'
}
}
undefined '"Dealer"'
{
foreignKey: { type: [Function: Noop], allowNull: false, name: 'dealers_did' }
foreignKey: {
type: [Function: Noop],
allowNull: false,
name: 'dealer_product_dpid'
}
}
这是说undefined '"Dealer"' 经销商我的字符串未定义。我该如何解决这个问题?
对于原始表,经销商是与 DealerProduct 关联的表
【问题讨论】:
标签: node.js sequelize.js chai sinon sinon-chai