node.js - How to write a post request test in mocha with data to test if response matches? -


question: how write post request test in mocha tests if response matches?

the response url string redirect 3rd party service.

working example payload:

curl -h "content-type: application/json" -x post -d '{"participant":{"nuid":"98asdf988sdf89sdf89989sdf9898"}}' http://localhost:9000/api/members 

member.controller.js // post method

// creates new member in 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         })     }   ); }; 

expected res.send

 {"redirecturl":"http://localhost:9000/registration/98asdf988sdf89sdf89989sdf9898"}   

working example request test

var should = require('should'); var app = require('../../app'); var request = require('supertest');  describe('get /api/members', function() {    it('should respond 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 redirect on post', function(done) {     // need here   }); }); 

try this:

it('should respond 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();       });   }); 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -