node.js无法在集成testing中上传文件

我在重构之前将集成testing添加到遗留代码库。 在这种情况下,上传一个文件。

考试:

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() }) }) 

正在testing的端点在生产中工作正常。 但为了让testing通过,我必须在multer模块的make-middleware.js中注释以下几行:

 if (!includeFile) { // appender.removePlaceholder(placeholder) // return fileStream.resume() } 

对节点没有经验,我一定错过了一些configuration或者其他东西。 我怎样才能使我的testing通过(无需修改外部模块的代码)?

multer使用multer来完成工作(get / stream文件)。 您评论的行只是停止stream:

这个代码中的fileStream.resume()stream.resume()代码中相当于stream.resume() ,所以它只是丢弃stream:

(从busboy文档):

你应该总是处理stream不pipe你是否关心文件内容(例如,你可以简单地做stream.resume();如果你想丢弃的内容)

但Multer不应该这样做!

如果你用一个将includeFile设置为false的callbackfileFilter传递给Multer一个自定义的fileFilter ,它只会performance这种行为。
否则,如果你没有fileFilter选项,Multer使用下面的默认fileFilter (什么都不做):

 function allowAll (req, file, cb) { cb(null, true) } 

正如你所看到的,callback的第二个参数是true ,它是includeFile

所以,你可以检查你的自定义fileFilter如果你有一个,如果你不这样做,这可能是一个意想不到的副作用,我祝你好运!

希望能帮助到你,
最好的祝福