gpt4 book ai didi

node.js - 错误 : ENOENT: no such file or directory, 使用 chai 打开 '../test/testFiles/shadab.jpeg' 文件上传测试用例

转载 作者:行者123 更新时间:2023-11-28 21:35:34 29 4
gpt4 key购买 nike

我正在尝试使用 Mocha/Chai 测试我的 Node API。我创建了一个“测试”文件夹,其中包含 File.js,里面有这段代码,同一个测试文件夹包含一个名为 testFile 的文件夹,其中我有一个图像“shadad.jpeg”。 test.js 文件包含以下代码

describe("/POST upload", () => {
it("it should POST a single file upload", done => {
chai
.request(server)
.post("/upload")
.field("Content-Type", "multipart/form-data")
.attach(
"file",
fs.readFileSync("./testFiles/shadab.jpeg"),
"shadab.jpeg"
)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a("object");
res.body.should.have.property("success").eql(true);
done();
});
});
});

但是当我执行命令 npm test 时,我得到了错误

Error: ENOENT: no such file or directory, open '../test/testFiles/shadab.jpeg'

我正在使用 PostMan 测试我的 api,并从表单数据键作为"file"发送,将值作为文件“shadab.jpeg”发送。并得到这个回应。

{
"success": true,
"data": "Added Successfully"
}

我在这里做错了什么?请帮忙。

最佳答案

我发现了这个问题 https://github.com/visionmedia/supertest/issues/259 .
This comment解决了我的问题。查看给定的 github 链接以获取更多详细信息。
通过使用像这样的文件的完整路径 fs.readFileSync(`${__dirname}/testFiles/shadab.jpeg`) 它也像 Pimento Web 在评论中提到的那样工作。相对文件路径在测试文件夹中表现异常。
我已经使用了 fs.readFileSync("test/testFiles/shadab.jpeg") 它现在对我有用。正如我在上面这个问题中提到的 test 文件夹是包含 testFiles 文件夹和 shadab.jpeg 位于 testFiles 文件夹中。

关于node.js - 错误 : ENOENT: no such file or directory, 使用 chai 打开 '../test/testFiles/shadab.jpeg' 文件上传测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59175043/

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