我正在向重构之前的遗留代码库添加集成测试。在这种情况下,上传文件。node.js无法在集成测试中上传文件
测试:
it('uploads a photo at the specified index', done => {
chai.request(server.instance)
.post('/profile/photo/0')
.set('Access-Token', `${token}`)
.set('API-Key', testConfig.apiKey)
.field({contentId: 'foobar'})
.attach('file', fs.readFileSync(__dirname + '/logo.png'), 'file')
.end((err, res) => {
console.log(JSON.stringify(res.body))
res.should.have.status(200)
done()
})
})
端点被测试生产工作正常。但要获得测试通过,我在multer
模块的make-middleware.js
注释掉以下行:
if (!includeFile) {
// appender.removePlaceholder(placeholder)
// return fileStream.resume()
}
作为缺乏经验与节点,我一定是错过了一些配置什么的。我怎样才能让我的测试通过(无需修改外部模块的代码)?
你100%确信'fs.readFileSync(__目录名称+ '/logo.png')'读取文件?顺便说一句:使用'path.join(__dirname,'logo.png')'是个好主意,所以它不会在Windows上失败。 – pawel
@pawel是的,绝对相信它被正确读取。好的关于窗户的提示,谢谢。 –
您是否使用了发布相同文件('logo.png')的端点(此测试之外)?似乎你已经在你的服务器上为你的服务器定义了一个文件过滤器,并且该文件没有通过它... – cviejo