mocha/chai 测试挂在等待 tojson().fromFile 上,邮递员通过

发布于 2025-01-11 09:36:36 字数 1438 浏览 5 评论 0原文

所以,基本上我正在开发我的第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文