How to unit test with a file upload in mocha

后端 未结 7 1188
走了就别回头了
走了就别回头了 2020-12-14 15:11

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

相关标签:
7条回答
  • 2020-12-14 16:06
    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();
            });
        });
    
    });
    
    0 讨论(0)
提交回复
热议问题