2016-03-23 7 views
2

ich einen Beitrag Endpunkt haben, die multer verwendet und nimmt eine Reihe von Dateien:Testing Express multer Datei-Array mit Supertest laden

router.post('/api/projects/:id/sessions', upload.array('files', 4), function(req, res, next) { 
    ... 
} 

In Test, ich brauche eine Reihe von 4-Dateien zu senden, kann ich nicht Finde heraus, wie du das mit Supertest machst. Hier ist mein Abschaltbetriebsart Test unter Verwendung eines Verfahrens sah ich an anderer Stelle vorgeschlagen:

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

Dies gibt eine ziemlich lange Fehler:

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) 

Kann mir jemand helfen, in einen Arbeits Test für diese POST Endpunkt zu schreiben? Vielen Dank.

Antwort

2

So stellt sich heraus, wenn Sie nur attach Dateien auf den gleichen Namen, werden sie als solche zu einem Array und geschickt angehängt werden, so dass der Test unter funktionierte gut:

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