gpt4 book ai didi

node.js - 如何在 Node.js 中的 Sequelize Mock 中为 Sequelize.js 原始查询编写测试用例

转载 作者:行者123 更新时间:2023-12-03 22:17:24 29 4
gpt4 key购买 nike

我无法理解使用 Sequelize Mock 在 sequelizejs 中为原始查询编写测试用例。

我的函数成功返回优惠券对象,状态为 200

try {
const domain = req.params.domain;
const domainFilter = { domain: { [Op.eq]: req.params.domain } };
const merchant = await models.merchants.findOne({ where: domainFilter });
if (merchant) {
const coupons = await models.sequelize.query(`SELECT * FROM coupons LEFT JOIN coupon_activities ON coupons.coupon=coupon_activities.coupon WHERE coupons.domain='${domain}' ORDER BY coupon_activities.best_discount DESC, coupon_activities.worked DESC, coupons.expire_date DESC LIMIT 50`, { type: sequelize.QueryTypes.SELECT });
if (coupons && coupons.length > 0) {
const keys = [...new Set(coupons[0].map(el => el.coupon))]
const filteredCoupons = coupons[0].filter(el => {
if (keys.indexOf(el.coupon) !== -1) {
keys.splice(keys.indexOf(el.coupon), 1)
return true;
} else {
return false;
}
});
res.status(200).send({ coupons: filteredCoupons });
} else {
res.status(204).send({ response: "No Coupons" });
}
} else {
res.status(204).send({ response: "No Content" });
}
} catch (err) {
console.log(err);
res.status(500).send({ response: "Error in server" });
}

函数 的测试用例
describe('Get Coupons By Merchand Id', () => {
it('It got all coupons of a merchant', (done) => {
const merchant_id = 1;
chai.request(server)
.get('/api/coupons/' + merchant_id)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.not.have.property('errors');
res.body.coupons.length.should.not.equal(0);
res.body.coupons[0].merchant_id.should.be.equal(merchant_id);
done();
});
});
});

模拟我已经设置了
'use strict';
const dbMock = require('./sequelize_mock');
const Op = dbMock.Op;

let SequelizeMock = dbMock.define("sequelize", {
merchant_id: 1,
domain: 'amazon.com',
coupon: 'TESTCOUPON',
description: 'This is a test',
expiry_date: '12-12-9999',
}, {
indexes: [{ fields: ['merchant_id'] }],
createdAt: false,
updatedAt: false,
id: false,
tableName: 'coupons',
});

SequelizeMock.$queryInterface.$useHandler(function(query, queryOptions, done) {
console.log('TCL: query', query)
console.log('TCL: queryOptions', queryOptions)
});

module.exports = SequelizeMock;

但它不起作用给我错误有人可以帮助我正确设置这个模拟所以我可以根据查询或有效的东西发回数据。

当前未定义错误 sequelize

最佳答案

同样在这里,我搜索了 sequelize-mock 文档,但没有任何相关内容。但是我在github上找到了这个例子:

If you're using the sequelize.query() function, you can queue up results on the mock sequelize object to be returned from that function as well. Like with models, you simply need to use the .$queueResult() method.



https://github.com/BlinkUX/sequelize-mock/issues/53

关于node.js - 如何在 Node.js 中的 Sequelize Mock 中为 Sequelize.js 原始查询编写测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55185549/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com