简体   繁体   中英

How to correct this test case in mocha framework?

I am trying to write test case for node application, which is using mocha as test framework.

test.js

var register = require('../routes/users');
var request = require('request');
var baseUrl = 'http://localhost:5000';

describe('registerUser()', function() {

    it('check email is already registered', function (done) {
        request.post({uri:baseUrl+'/register', form :{
            username: 'test',
            email: 'test@test.com'
        }}, function (e, res, body) {
            res.should.have.property('statusCode', 201);
            res.should.have.property('regErr', 'This email is already taken!');
        })
    });

})

The regErr is given as parameter in function registerUser on render. I expected the parameter regErr will be set as property of response and can be get in test. Please check my registerUser function in github. I am trying create a test case for this function.

Above code doesn't have property called regErr in response fetched in test case.

How to correct so that the rendering parameters can also be property in response?

Shall I need to change the actual function registerUser to get this? If so, how can I achieve this?

When doing an HTTP request, the callback function is called with three arguments:

  • err (Possible error return)
  • res (HTTPRequest object)
  • body (Buffer of the resulting body)

Therefore, regErr is in the body variable. And since you are rendering an HTML page, you're gonna have to parse the body yourself to find it. A possible solution would be to render JSON and use JSON.parse() on the resulting body buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM