如何在mocha中使用文件上传进行单元测试

Mar*_*yen 32 node.js express

我有一个基于Express.js构建的应用程序,我想测试文件上传功能.我正在尝试重现解析为req.files的对象(当使用express.bodyParser中间件时).我怎样才能做到这一点?

Jac*_*ack 44

以下是如何使用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();
              });
    });
});
Run Code Online (Sandbox Code Playgroud)

  • 当我尝试这个时,req.files仍未定义.我正在使用bodyParser并且文件没有ENOENT错误. (3认同)

Jes*_*per 13

你可以直接在摩卡这样做,但这有点棘手.这是发布图像的示例:

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()
  })
Run Code Online (Sandbox Code Playgroud)

Content-Disposition 的name参数是你的文件可以通过req.files访问的(所以我的例子是req.files.image)你也可以使用这样的数组值:name ="images []"和你的文件(s)将通过数组提供,例如:req.files.images [0]

此外,如果你还没有使用它,你应该看看这个(使mocha/express测试更容易):https: //github.com/visionmedia/express/blob/master/test/support/http .js文件

编辑:自快递3-beta5,快递使用supertest.要查看旧的http.js代码,请查看:https://github.com/visionmedia/express/blob/3.0.0beta4/test/support/http.js 或者只需转到supertest ..


小智 9

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

});
Run Code Online (Sandbox Code Playgroud)


Jun*_*ang 5

将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({field1: 'xxx', field2: 'yyy'})
              .attach('file', 'path/to/file.jpg')
              .end(function(err, res) {
                  res.should.have.status(200) // 'success' status
                  done()
              });
    });
});
Run Code Online (Sandbox Code Playgroud)