即使真实请求正确返回,NodeJS Nock测试仍失败

时间:2018-10-23 20:52:08

标签: node.js request nock

我正在发送API请求并获得有效的响应。这是我的路线(/测试):

module.exports = {
    test: function(req, res) {
      var http = require('http');
      var nock = require('nock');
      var request = require('request');

      nock('http://greetings_api:3000')
        .defaultReplyHeaders({
          'Content-Type': 'application/json'
        })
        .post('/greeting', {'language': 'es' })
        .reply(200, 'Hola');

      var options = {
        uri: 'http://greetings_api:3000/greeting',
        hostname: 'greetings_api',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'language': 'es'
        }
      };

      request(options, function (error, response, body) {
        console.log(response.body);
        res.send(response.body + ' tester');
      });
    }
};

这是我的考试:

const expect = require('chai').expect;

const { test } = require('../../routes/test');

let req = {
    body: {},
};

let res = {
    sendCalledWith: '',
    send: function(arg) {
        this.sendCalledWith = arg;
    }
};

describe('Test Route', function() {
    describe('Test() function', function() {
        it('Should welcome us', function() {
            test(req, res);
            expect(res.sendCalledWith).to.contain('Hola tester');
        });
    })
});

我收到以下错误:

  1) Test Route
       Test() function
         Should welcome us:
     AssertionError: expected '' to include 'Hola tester'
      at Context.<anonymous> (/tests/routes/app.test.js:20:43)

测试似乎没有正确地从路由中获取响应。我怎样才能使测试与Nock一起使用?

0 个答案:

没有答案