【发布时间】:2017-09-28 03:19:50
【问题描述】:
我在重构之前将集成测试添加到旧代码库。对于这种情况,上传文件。
测试:
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(__dirname + '/logo.png')会读取文件?顺便说一句:使用path.join( __dirname, 'logo.png' )是个好主意,这样它就不会在 Windows 上失败。 -
@pawel 是的,绝对相信它被正确读取。关于 windows 的好提示,谢谢。
-
您是否使用过端点(在此测试之外)发布了相同的文件 (
logo.png)。好像您已经在服务器上为 multer 定义了一个文件过滤器,但该文件没有通过它...
标签: javascript node.js multer