您可以直接在Mocha中执行此操作,但这有点棘手。这是发布图像的示例:
var filename = 'x.png' , boundary = Math.random()request(app) .post('/g/' + myDraftGallery._id) .set('Content-Type', 'multipart/form-data; boundary=' + boundary) .write('--' + boundary + 'rn') .write('Content-Disposition: form-data; name="image"; filename="'+filename+'"rn') .write('Content-Type: image/pngrn') .write('rn') .write(fs.readFileSync('test/'+filename)) .write('rn--' + 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]
- 另外,如果您还没有使用过它,那么应该看看一下(使mocha /
- express测试〜bit〜更加容易):https
- //github.com/visionmedia/express/blob/master/test/support/http
.js
编辑 :由于express
3-beta5,express使用supertest。要查看旧的http.js代码,请点击此处:https
:
//github.com/visionmedia/express/blob/3.0.0beta4/test/support/http.js
或直接移至超级测试。



