Domanda: Come scrivere un test di richiesta post in mocha che verifica se la risposta corrisponde?Come scrivere un test di richiesta post in mocha con i dati per verificare se la risposta corrisponde?
La risposta sarà solo una stringa di URL poiché è un reindirizzamento per un servizio di terze parti.
lavoro Esempio Carico utile:
curl -H "Content-Type: application/json" -X POST -d '{"participant":{"nuid":"98ASDF988SDF89SDF89989SDF9898"}}' http://localhost:9000/api/members
member.controller.js // metodo post
// 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 Previsto
{"redirectUrl":"http://localhost:9000/registration/98ASDF988SDF89SDF89989SDF9898"}
Esempio di lavoro richiesta GET test
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
});
});
Appena confuso con superagente;) – javierfdezg
cosa significa la variabile 'app'? –
'TypeError: request (...). Post (...). Send non è una funzione' –