mocha

2012-04-12 13 views
25

में फ़ाइल अपलोड के साथ इकाई परीक्षण कैसे करें मेरे पास Express.js पर बनाया गया एक ऐप है और मैं फ़ाइल अपलोड कार्यक्षमता का परीक्षण करना चाहता हूं। मैं req.files (Express.bodyParser midware का उपयोग करते समय) पर पार्स किए गए ऑब्जेक्ट को पुन: उत्पन्न करने का प्रयास कर रहा हूं। मैं यह कैसे कर सकता हूँ?mocha

उत्तर

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

संपादित करें: एक्सप्रेस 3-बीटा 5 के बाद, एक्सप्रेस सुपरटेस्ट का उपयोग करता है। वर्ष http.js कोड देखने के लिए यहाँ देखो: https://github.com/visionmedia/express/blob/3.0.0beta4/test/support/http.js या बस Supertest की अवधि में ..

+0

। लिखना अपरिभाषित – dianz

1

आप zombie.js https://github.com/assaf/zombie का उपयोग करने का प्रयास कर सकते हैं, यह मूल कार्यक्षमता के साथ परीक्षण के लिए एक आभासी ब्राउज़र बनाता है। यह एक विशिष्ट इनपुट क्षेत्र के लिए कोई फ़ाइल अनुलग्न और कुकीज़ और सत्र

संबंधित सार का समर्थन करता है सकते हैं: https://gist.github.com/764536

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 त्रुटियों का उपयोग कर रहा हूँ। –

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

वास्तव में मेरे लिए इसे हल नहीं करता है, अभी भी 'req.files.file' – knutole

+1

नहीं मिला है क्या आपने कभी यह @knutole हल किया है? –

+0

@AlexChin हां, धन्यवाद। (याद रखें कैसे।) – knutole

संबंधित मुद्दे

 संबंधित मुद्दे