【问题标题】:Testing REST API - req.body undefined (Node.js / Express / Mocha / Supertest)测试 REST API - req.body undefined (Node.js / Express / Mocha / Supertest)
【发布时间】:2015-03-03 07:03:35
【问题描述】:

我正在尝试测试 Node.js 中内置的 REST API。我已经用 Postman 手动测试了 API,没有任何问题,但是我在用 Mocha/Chai/Supertest 编写测试时遇到了麻烦。

当我尝试测试 POST 到路由时,请求正文未定义。到目前为止,在我的研究中,我似乎无法找到与我所做的与其他人所做的任何有意义的区别,但由于某种原因,我尝试发送的数据没有通过。

以下是路由的处理方式:

    router.route('/media')
        .post(RouteController.postMedia);


    RouteController.postMedia = function(req, res) {
        var media = new Media();

        console.log(req.body);

        media.title         = req.body.title;
        media.type          = req.body.type;
        media.tags          = req.body.tags;
        media.pubdate       = req.body.pubdate;
        media.editdate      = req.body.editdate;
        media.filename      = req.body.filename;
        media.extension     = req.body.extension;
        media.description   = req.body.description;

        media.save(function(err) {
            if (err)
                res.send(err);

            res.json({ message: 'File added!', data: media });
        });
    };

这是我的测试:

    var request     = require('supertest'),
        should      = require('chai').should(),
        express     = require('express'),
        mongoose    = require('mongoose'),
        app         = express();

    require('../api/routes')(app);

    var db = require('./config/db');

    describe('API Routing', function() {
        before(function(done) {
            mongoose.createConnection(db.url);
            done();
      });

        describe('Media', function () {
            it('should add new photo to database with call to POST /api/v1/media', function(done) {
                var photo = {
                    title: 'Test Photo', 
                    type: 'photo', 
                    tags: ['test', 'test2', 'asdf'], 
                    filename: 'test-photo.jpg', 
                    description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
                };

                request(app)
                    .post('/api/v1/media')
                    .send(photo)
                    .end(function(err, res) {
                        if (err) {
                            throw err
                        }

                        res.status.should.equal(200);
                        done();
                    });
            });
    });

当我运行测试时,我收到错误TypeError: Cannot read property 'title' of undefined,因为 req.body 未定义。 console.log(req.body); 部分确认 req.body 未定义。

【问题讨论】:

    标签: javascript node.js express mocha.js supertest


    【解决方案1】:

    这里有两种可能性(嗯,一种确定,一种可能性):

    首先,您需要告诉supertest您正在发送 JSON 类型(或您发送的任何其他类型)。

                  request(app)
                    .post('/api/v1/media')
                    .type('json')
                    .send(photo)
    

    第二,你有没有在node中设置bodyParser?不确定您使用的是什么框架,但它们都需要某种形式的正文解析,无论是内置的还是外部的。 https://www.npmjs.com/package/body-parser

    【讨论】:

    • 我读到如果我没有指定类型,Supertest 将默认发送为application/x-www-form-urlencoded。为了确定,我会补充一点。我认为您确实发现了我对 bodyParser 的问题。实际上,我确实在我的 index.js 文件中进行了设置,这就是为什么当我手动测试 API 时 API 可以工作,但我没有将它包含在我的测试中。我有一种感觉,这将是一个这样的疏忽。感谢您的帮助!
    • 正文解析器线索帮助我解决了这个问题
    【解决方案2】:

    我也遇到了同样的问题。我已通过添加以下行来解决此问题:

    app.use(bodyParser.json());

    【讨论】:

      猜你喜欢
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多