最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - res.should.have.status gives me error - Stack Overflow

programmeradmin3浏览0评论

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

describe('Local signup', function() {
    it('should return error trying to save duplicate username', function(done) {
      var profile = {
        email: '[email protected]',
        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 have declared var should = require('should'); , my ide notifies me that 'should' is a unused local variable. I don't really know why.

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

describe('Local signup', function() {
    it('should return error trying to save duplicate username', function(done) {
      var profile = {
        email: '[email protected]',
        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 have declared var should = require('should'); , my ide notifies me that 'should' is a unused local variable. I don't really know why.

Share Improve this question edited Jan 6, 2015 at 2:12 Charlie asked Jan 6, 2015 at 1:56 CharlieCharlie 811 silver badge7 bronze badges 5
  • Are you running this in a browser? – plalx Commented Jan 6, 2015 at 2:02
  • @plalx Tagged with node.js. – Yury Tarabanko Commented Jan 6, 2015 at 2:05
  • @YuryTarabanko I know, but it says in the docs that status is not part of the browser build, so I got suspicious. – plalx Commented Jan 6, 2015 at 2:05
  • @plalx I run the test in terminal – Charlie Commented Jan 6, 2015 at 2:11
  • res.should.have.status() is something which is available only with some implementation of the should idiom. In other words, the error may happen because the package used to provide should does not implement status, or because the package has not been correctly initialized, or for some other reason. Nothing in this question indicates what package provides should. – Louis Commented Jan 6, 2015 at 11:55
Add a comment  | 

4 Answers 4

Reset to default 16

Try

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

or

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

And about " 'should' is a unused local variable". It's true. You don't use should directly. Only sideeffects. Try require('should'); instead.

Place the line:

require('should-http');

somewhere in your code. E.g.:

require('should-http');

describe('Test Something', function() {
    ...

As an addition to Yury answer. There is should-http package, which contain .status(code) assertion. You need to require somewhere it in code and it will be added to should.js.

I would switch to expect:

expect(res.status).to.be.eq(400);
发布评论

评论列表(0)

  1. 暂无评论