mocha/chai 测试挂在等待 tojson().fromFile 上,邮递员通过
所以,基本上我正在开发我的第一个 MERN 堆栈项目,其中我有一个 api 端点,可以重置收集车辆。我从集合中删除数据,然后解析 csv 中的数据并将它们转换为 json 并插入新数据。当运行我的后端代码时,postman 和 robo3T 中的一切都运行顺利。但是当我运行测试代码时,执行挂在指令处:
const cars_model = wait tojson().fromFile(vehicles_csv);
因此测试失败(返回 res.status 500)。此外,在运行测试代码时,端点也无法在邮递员中运行。我做错了什么?
PS:这是我第一次在stackoverflow上写文章,如果我还没有把问题说清楚,我很乐意详细说明:)
这是我的端点
exports.postResetVehicles = async (req, res) => {
const vehicles_csv = "vehicles.csv";
try {
Vehicles.deleteMany({}).then(function () {
console.log("Data deleted and");
});
const vehicles_model = await tojson().fromFile(vehicles_csv);
Vehicles.insertMany(vehicles_model).then(function () {
console.log("data inserted"); // Success
console.log("=>Data reseted");
}).catch(function (error) {
console.log(error); // Failure
});
return res.status(200).json({ "status": "OK" });
} catch (err) {
return res.status(500).json({ "status": "failed", "error": err });
}
};
这是我的测试代码
describe("Testing 'admin/resetvehicles'", () => {
it("should succeed if Vehicles collection is reset", async () => {
const response = await request(app).post(
"/interoperability/api/admin/resetvehicles"
);
expect(response.status).to.equal(200)
});
});
So, basically I am developing my first MERN stack project in which I have an api endpoint which resets the collection vehicles. I delete the data from the collection, then I parse the data from a csv and convert them to json and insert the new data. When running my backend code, everything runs smoothly in postman and robo3T. But when I run my testing code, the execution hangs at the instruction:
const vehicles_model = await tojson().fromFile(vehicles_csv);
and consequently the testing fails (res.status 500 is returned). Also, while running the testing code, the endpoint fails to run in postman, as well. What am I doing wrong?
PS: It is my first time writing on stackoverflow, if I haven't made the problem clear, I'll be happy to elaborate :)
this is my endpoint
exports.postResetVehicles = async (req, res) => {
const vehicles_csv = "vehicles.csv";
try {
Vehicles.deleteMany({}).then(function () {
console.log("Data deleted and");
});
const vehicles_model = await tojson().fromFile(vehicles_csv);
Vehicles.insertMany(vehicles_model).then(function () {
console.log("data inserted"); // Success
console.log("=>Data reseted");
}).catch(function (error) {
console.log(error); // Failure
});
return res.status(200).json({ "status": "OK" });
} catch (err) {
return res.status(500).json({ "status": "failed", "error": err });
}
};
and this is my testing code
describe("Testing 'admin/resetvehicles'", () => {
it("should succeed if Vehicles collection is reset", async () => {
const response = await request(app).post(
"/interoperability/api/admin/resetvehicles"
);
expect(response.status).to.equal(200)
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论