2012-04-12 40 views

Trả lời

10

Bạn có thể làm điều này trực tiếp trong Mocha nhưng đó là một chút khéo léo. Dưới đây là một ví dụ đăng một hình ảnh:

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

Các tên tham số của Content-Disposition là những gì tập tin của bạn sẽ được truy cập như qua req.files (vì vậy req.files.image ví dụ của tôi) Bạn cũng có thể sử dụng giá trị mảng như sau: name = "images []" và (các) tệp của bạn sẽ có sẵn qua mảng, ví dụ: req.files.images [0]

Ngoài ra nếu bạn chưa sử dụng bạn nên xem xét điều này (làm cho thử nghiệm mocha/express nhanh hơn ~ bit ~ dễ dàng hơn): https://github.com/visionmedia/express/blob/master/test/support/http.js

Chỉnh sửa: Kể từ khi thể hiện 3-beta5, sử dụng nhanh nhất siêu cấp. Để xem mã http.js cũ, hãy xem tại đây: https://github.com/visionmedia/express/blob/3.0.0beta4/test/support/http.js Hoặc đơn giản là chuyển sang supertest ..

+0

.write is undefined – dianz

1

Bạn có thể thử sử dụng zombie.js https://github.com/assaf/zombie, nó tạo trình duyệt ảo để thử nghiệm với chức năng cơ bản. Nó có thể đính kèm một tập tin vào một lĩnh vực đầu vào cụ thể và hỗ trợ các tập tin cookie và các buổi

ý chính liên quan: https://gist.github.com/764536

39

Dưới đây là ví dụ về cách bạn làm điều đó với mô-đun siêu lớn nhất.

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

Khi tôi thử điều này, req.files vẫn chưa được xác định. Tôi đang sử dụng bodyParser và không có lỗi ENOENT cho tệp. –

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

Thay đổi đính kèm ('image') để đính kèm ('file') sẽ giải quyết vấn đề của req.files.file không xác định.

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

thực sự không giải quyết nó cho tôi, vẫn không nhận được 'req.files.file' – knutole

+1

Bạn đã bao giờ giải quyết @knutole này chưa? –

+0

@AlexChin Vâng, cảm ơn. (Đừng nhớ làm thế nào.) – knutole

Các vấn đề liên quan