NodeJS Тест Nock не пройден, хотя реальный запрос возвращается корректно - PullRequest
0 голосов
/ 23 октября 2018

Я отправляю запрос API и получаю правильный ответ.Вот мой маршрут (/ test):

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?

...