Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

node.js - How to unit test with a file upload in mocha

I have an app built on Express.js and I'd like to test the file upload functionality. I'm trying to reproduce the object parsed to req.files (when using express.bodyParser middleware). How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is an example of how you would do it with supertest module.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...