Как написать тестовый запрос на сообщение в мокко с данными для проверки соответствия ответа?

Вопрос: Как я могу написать тестовый запрос на сообщение в мокко, который проверяет, соответствует ли ответ?

Ответ будет просто строкой url, так как это перенаправление для сторонней службы.

Рабочий пример Полезная нагрузка:

curl -H "Content-Type: application/json" -X POST -d '{"participant":{"nuid":"98ASDF988SDF89SDF89989SDF9898"}}' http://localhost:9000/api/members

member.controller.js//метод отправки

// Creates a new member in the DB.
exports.create = function(req, res) {
  Member.findByIdAndUpdate(req.body.participant.nuid,
    { "$setOnInsert": { "_id": req.body.participant.nuid } },
      { "upsert": true },
      function(err,doc) {
        if (err) throw err;
        res.send({
          'redirectUrl': req.protocol + '://' + req.get('host') + '/registration/' + req.body.participant.nuid
        })
    }
  );
};

Ожидаемый res.send

 {"redirectUrl":"http://localhost:9000/registration/98ASDF988SDF89SDF89989SDF9898"}  

Рабочий пример Тест запроса GET

var should = require('should');
var app = require('../../app');
var request = require('supertest');

describe('GET /api/members', function() {

  it('should respond with JSON array', function(done) {
    request(app)
      .get('/api/members')
      .expect(200)
      .expect('Content-Type', /json/)
      .end(function(err, res) {
        if (err) return done(err);
        res.body.should.be.instanceof(Array);
        done();
      });
  });
  it('should respond with redirect on post', function(done) {
    // need help here
  });
});

Ответы

Ответ 1

Попробуйте следующее:

it('should respond with redirect on post', function(done) {
    request(app)
      .post('/api/members')
      .send({"participant":{"nuid":"98ASDF988SDF89SDF89989SDF9898"}})
      .expect(200)
      .expect('Content-Type', /json/)
      .end(function(err, res) {
        if (err) done(err);
        res.body.should.have.property('participant');
        res.body.participant.should.have.property('nuid', '98ASDF988SDF89SDF89989SDF9898');
        done();
      });
  });

Ответ 2

Вы также можете установить тип "form" и тип контента json, как показано ниже:

it("returns a token when user and password are valid", (done) => {
    Users.createUserNotAdmin().then((user: any) => {
        supertestAPI
        .post("/login")
        .set("Connection", "keep alive")
        .set("Content-Type", "application/json")
        .type("form")
        .send({"email": user.email, password: "123456"})
        .end((error: any, resp: any) => {
            chai.expect(JSON.parse(resp.text)["token"].length).above(400, "The token length should be bigger than 400 characters.");
            done();
        })
    });
});

Вы также должны установить body-parser при создании сервера, как показано ниже:

 server.use(bodyParser.urlencoded({ extended: false }));
 server.use(bodyParser.json());