2012-04-12 49 views

回答

10

您可以直接在摩卡做到這一點,但它是一個有點棘手。這裏有一個例子發佈的圖像:

var filename = 'x.png' 
    , boundary = Math.random() 

request(app) 
    .post('/g/' + myDraftGallery._id) 
    .set('Content-Type', 'multipart/form-data; boundary=' + boundary) 
    .write('--' + boundary + '\r\n') 
    .write('Content-Disposition: form-data; name="image"; filename="'+filename+'"\r\n') 
    .write('Content-Type: image/png\r\n') 
    .write('\r\n') 
    .write(fs.readFileSync('test/'+filename)) 
    .write('\r\n--' + boundary + '--') 
    .end(function(res){ 
    res.should.have.status(200) 
    done() 
    }) 

內容處置的參數是你的文件將作爲通過req.files訪問(所以req.files.image我的例子) 你也可以使用像這樣的數組值:name =「images []」,你的文件將通過數組提供,例如:req.files.images [0]

此外,如果你還沒有使用它你應該看看這個(使摩卡/表達測試更容易〜): https://github.com/visionmedia/express/blob/master/test/support/http.js

編輯:由於express 3-beta5,express使用超級。要看看舊的http.js代碼看看這裏:https://github.com/visionmedia/express/blob/3.0.0beta4/test/support/http.js 或者乾脆移動到超特技。

+0

.write was undefined – dianz 2013-07-03 01:57:43

39

下面是一個如何使用超級模塊來做的例子。

var should = require('should'), 
    supertest = require('supertest'); 
var request = supertest('localhost:3000'); 

describe('upload', function() { 
    it('a file', function(done) { 
     request.post('/your/endpoint') 
       .field('extra_info', '{"in":"case you want to send json along with your file"}') 
       .attach('image', 'path/to/file.jpg') 
       .end(function(err, res) { 
        res.should.have.status(200); // 'success' status 
        done(); 
       }); 
    }); 
}); 
+3

當我嘗試這樣做時,req.files仍未定義。我正在使用bodyParser,並且文件沒有ENOENT錯誤。 – 2015-03-01 18:01:09

6
var expect = require('expect.js'); 
supertest = require('supertest'); 
var request = supertest('localhost:3000'); 

describe('create album', function() { 
    it('valid ', function (done) { 
     request.post('/albums') 
      .set('Authorization', 'Token eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.IjkxMTg3NTk1ODg2MCI.gq32xfcOhv5AiZXJup5al1DGG0piyGWnrjZ5NouauCU') 
      .field('Content-Type', 'multipart/form-data') 
      .field('name', 'moni') 
      .field('description', 'Nature+Pics') 
      .field('caption', 'nature') 
      .field('contacts', '["' + 911354971564 + '","' + 919092888819 + '"]') 
      .field('dimensions', '{"photo1":{"height": 10, "width": 10}, "photo2":{"height": 20, "width": 20}, "photo3":{"height": 20, "width": 20}, "photo4":{"height": 20, "width": 20}, "photo5":{"height": 20, "width": 20}}') 
      .attach('photo1', '/home/monica/Desktop/pic/1.jpeg') 
      .attach('photo2', '/home/monica/Desktop/pic/2.jpeg') 
      .attach('photo3', '/home/monica/Desktop/pic/3.jpeg') 
      .attach('photo4', '/home/monica/Desktop/pic/4.jpeg') 
      .attach('photo5', '/home/monica/Desktop/pic/5.jpeg') 
      .end(function (err, res) { 
      if (err) { 
       console.log(err); 
      } else expect(res.status).to.equal(200); 
      done(); 
     }); 
    }); 

}); 
3

改變附加( '圖像')附加( '文件')將解決沒有定義req.files.file的問題。

var should = require('should'), 
    supertest = require('supertest'); 
var request = supertest('localhost:3000'); 

describe('upload', function() { 
    it('a file', function(done) { 
     request.post('/your/endpoint') 
       .field('extra_info', '...') 
       .attach('file', 'path/to/file.jpg') 
       .end(function(err, res) { 
        res.should.have.status(200) // 'success' status 
        done() 
       }); 
    }); 
}); 
+0

實際上doens't解決它對我來說,仍然沒有得到'req.files.file' – knutole 2016-01-12 18:14:08

+1

你有沒有解決這個@knutole? – 2016-06-21 19:27:24

+0

@AlexChin是的,謝謝。 (不記得怎麼做。) – knutole 2016-06-22 09:55:51

相關問題