我有一个使用multer的post端点并获取一个文件数组:
router.post('/api/projects/:id/sessions', upload.array('files', 4), function(req, res, next) {
...
}
在测试中,我需要发送一个包含4个文件的数组,我无法弄清楚如何使用supertest执行此操作。这是我使用我在其他地方建议的方法的非工作测试:
it('Add a session', function(done) {
api.post('/api/projects/' + projectId + '/sessions')
.set('X-Auth', tokenA)
.attach('files[0]', './test/files/d.zip')
.attach('files[1]', './test/files/m.csv')
.attach('files[2]', './test/files/o.15o.txt')
.attach('files[3]', './test/files/e.n')
.end(function(err, res) {
expect(res.status).to.equal(201)
done(err)
})
})
这会返回一个相当长的错误:
Error: Unexpected field
at makeError (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/lib/make-error.js:12:13)
at wrappedFileFilter (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/index.js:39:19)
at Busboy.<anonymous> (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/lib/make-middleware.js:112:7)
at emitMany (events.js:108:13)
at Busboy.emit (events.js:182:7)
at Busboy.emit (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/lib/main.js:31:35)
at PartStream.<anonymous> (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/lib/types/multipart.js:208:13)
at emitOne (events.js:77:13)
at PartStream.emit (events.js:169:7)
at HeaderParser.<anonymous> (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/Dicer.js:51:16)
at emitOne (events.js:77:13)
at HeaderParser.emit (events.js:169:7)
at HeaderParser._finish (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/HeaderParser.js:68:8)
at SBMH.<anonymous> (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/HeaderParser.js:40:12)
at emitMany (events.js:108:13)
at SBMH.emit (events.js:182:7)
at SBMH._sbmh_feed (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/node_modules/streamsearch/lib/sbmh.js:159:14)
at SBMH.push (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/node_modules/streamsearch/lib/sbmh.js:56:14)
at HeaderParser.push (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/HeaderParser.js:46:19)
at Dicer._oninfo (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/Dicer.js:197:25)
at SBMH.<anonymous> (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/Dicer.js:127:10)
at emitMany (events.js:108:13)
at SBMH.emit (events.js:182:7)
at SBMH._sbmh_feed (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/node_modules/streamsearch/lib/sbmh.js:188:10)
at SBMH.push (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/node_modules/streamsearch/lib/sbmh.js:56:14)
at Dicer._write (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/Dicer.js:109:17)
at doWrite (_stream_writable.js:292:12)
at writeOrBuffer (_stream_writable.js:278:5)
at Dicer.Writable.write (_stream_writable.js:207:11)
at Multipart.write (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/lib/types/multipart.js:285:24)
at Busboy._write (/Users/Oscar/Google Drive/CompSci/COMPSCI-YEAR4/FinalProject/gpsapp/node_modules/multer/node_modules/busboy/lib/main.js:74:16)
at doWrite (_stream_writable.js:292:12)
at writeOrBuffer (_stream_writable.js:278:5)
at Busboy.Writable.write (_stream_writable.js:207:11)
at IncomingMessage.ondata (_stream_readable.js:528:20)
at emitOne (events.js:77:13)
at IncomingMessage.emit (events.js:169:7)
at IncomingMessage.Readable.read (_stream_readable.js:360:10)
at flow (_stream_readable.js:743:26)
at resume_ (_stream_readable.js:723:3)
at doNTCallback2 (node.js:439:9)
at process._tickCallback (node.js:353:17)
任何人都可以帮我写这个POST端点的工作测试吗?感谢。
答案 0 :(得分:7)
所以事实证明,如果你只是attach
个同名文件,它们会被附加到一个数组并发送,所以下面的测试工作正常:
it('Add a session', function(done) {
api.post('/api/projects/' + projectId + '/sessions')
.set('X-Auth', tokenA)
.attach('files', './test/files/d.zip')
.attach('files', './test/files/m.csv')
.attach('files', './test/files/o.15o.txt')
.attach('files', './test/files/e.n')
.end(function(err, res) {
expect(res.status).to.equal(201)
done(err)
})
})