提问人:Nkechi Anyanwu 提问时间:10/30/2023 更新时间:10/30/2023 访问量:12
具有生成时间戳的数据库对象的 Javascript AssertionError
Javascript AssertionError for database objects with generated timestamps
问:
我断言从我的数据库中获取的数据包含以下对象,但我的 assertionError 似乎表明这些对象不包括在内。我的查询正在记录到控制台,显示唯一的区别是时间戳。这是我的测试代码:
const chai = require("chai");
const request = require("supertest");
const { server } = require("../../app");
const { expect } = chai;
const shouldNotRestockProduct = {
id: 1,
name: "Salmon",
price: 10.0,
stock: 5,
min_stock: 2,
created_at: '2023-10-30TT00:00:00.000Z',
updated_at: '2023-10-30TT00:00:00.000Z',
shouldRestock: 'no',
};
const shouldRestockProduct = {
id: 2,
name: "Chicken",
price: 6.5,
stock: 5,
min_stock: 10,
created_at: "2023-10-30TT00:00:00.000Z",
updated_at: "2023-10-30TT00:00:00.000Z",
shouldRestock: 'yes',
};
const shouldShortlyStockProduct = {
id: 3,
name: "Turkey",
price: 5.5,
stock: 5,
min_stock: 5,
created_at: "2023-10-30TT00:00:00.000Z",
updated_at: "2023-10-30TT00:00:00.000Z",
shouldRestock: 'shortly',
};
describe("Fetch products test", async() => {
it("shows all stock states", async () => {
const { body, status } = await request(server).get("/products");
const { data } = body;
expect(status).to.equal(200);
expect(data).to.deep.include(shouldNotRestockProduct);
expect(data).to.deep.include(shouldRestockProduct);
expect(data).to.deep.include(shouldShortlyStockProduct);
});
});
下面是用于设置测试数据库的代码,我对此运行测试:
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface, Sequelize) {
return queryInterface.sequelize.transaction(async (t) => {
await queryInterface.bulkInsert("Products", [
{
name: "Salmon",
price: 10.0,
stock: 5,
min_stock: 2,
created_at: new Date(),
updated_at: new Date(),
},
{
name: "Chicken",
price: 6.5,
stock: 5,
min_stock: 10,
created_at: new Date(),
updated_at: new Date(),
},
{
name: "Turkey",
price: 5.5,
stock: 5,
min_stock: 5,
created_at: new Date(),
updated_at: new Date(),
},
])
})
},
async down (queryInterface, Sequelize) {
return queryInterface.sequelize.transaction(async (t) => {
await queryInterface.bulkDelete("Products", null, {})
})
}
};
当我针对test_database运行测试时,出现以下错误:
- 提取产品测试显示所有库存状态:AssertionError: expected [ ...(3) ] 深度包含 { id: 1, name: 'Salmon', ...(6) }
我希望断言会通过,因为对象是相同的,但问题可能是时间戳不同吗?我正在测试 Sequelize,并遵循了这个似乎对他们有效的教程:ExpressJS 和 Sequelize 应用程序使用 mocha、chai、supertest、migrations 和 seeds 进行了测试
答: 暂无答案
评论