Res.should.have.status gives me an error

I am new to mocha and should.js. I'm trying to check the status of a response, but it gives me a TypeError: Object #<Assertion> has no method 'status' The code looks like this:

 describe('Local signup', function() { it('should return error trying to save duplicate username', function(done) { var profile = { email: ' abcd@abcd.com ', password: 'Testing1234', confirmPassword: 'Testing1234', firstName: 'Abc', lastName: 'Defg' }; request(url) .post('/user/signup') .send(profile) .end(function(err, res) { if (err) { throw err; } res.should.have.status(400); done(); }); }); 

I also noticed that although I declared var should = require('should'); my ide informs me that 'should' is an unused local variable. I really don't know why.

+6
source share
4 answers

Try

 res.status.should.be.equal(400); 

or

  res.should.have.property('status', 400); 

And about "'should' is an unused local variable." It's true. You should not use it directly. Only side effects. Instead, try require('should'); .

+13
source

As an addition to Yuri's answer. The should-http package containing the .status(code) statement. You need to require it somewhere in the code, and it will be added to the should.js file.

+2
source

Put the line:

 require('should-http'); 

somewhere in your code. For instance:.

 require('should-http'); describe('Test Something', function() { ... 
+2
source

I would switch to expect :

 expect(res.status).to.be.eq(400); 
0
source

Source: https://habr.com/ru/post/980557/


All Articles