如何用摩卡上传文件进行unit testing

我有一个基于Express.js的应用程序,我想testingfile uploadfunction。 我想重现parsing到req.files的对象(当使用express.bodyParser中间件时)。 我怎样才能做到这一点?

你可以直接在摩卡做这个,但这有点棘手。 以下是发布图片的示例:

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

Content-Disposition的name参数就是你的文件可以通过req.files访问的东西(例如对于我的例子req.files.image)你也可以使用像这样的数组值:name =“images []”和你的文件(s)将通过数组可用,例如:req.files.images [0]

另外,如果你还没有使用它,你应该看看这个(使摩卡/快速testing~~~~~~~~~~~~~~~~~~)更容易: https : //github.com/visionmedia/express/blob/master/test/support/http .js文件

编辑 :由于快递3-beta5,快递使用超级。 看看旧的http.js代码看看这里: https : //github.com/visionmedia/express/blob/3.0.0beta4/test/support/http.js或者干脆移动到supertest ..

这里是一个如何使用超级模块来做的例子。

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

刚刚通过TJ发现了这个模块: https : //github.com/visionmedia/supertest 。

将attach('image')更改为attach('file')将解决未定义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() }); }); }); 

您可以尝试使用zombie.js https://github.com/assaf/zombie ,它创build了一个虚拟浏览器,用于testing基本function。 它可以将文件附加到特定的input字段,并支持cookie和会话

相关的要点: https : //gist.github.com/764536