Mocha/Should 'undefined is not a function'

Mocha/Should 'undefined is not a function'

我要结束 this 教程,尝试使用 Mocha、Supertest 和 Should.js 进行测试。

我有以下基本测试来通过 PUT 端点创建一个用户,该端点接受 headers 中的数据。

describe('User Routes', function () {
    it('should allow me to make my user', function (done) {
        request(url)
        .put('/users')
        .set(myCreds)
        // end handles the response
        .end(function(err, res) {
              if (err) {
                throw err;
              }
              // this is should.js syntax, very clear
              res.should.have.status(201);
              done();
            });
        });

然而,虽然端点确实触发了,并且用户确实成功了,但代码会抛出一个错误,指出 should 未定义... Uncaught TypeError: undefined is not a function.

我有

var should = require('should');
var assert = require('assert');
var request = require('supertest');

在文件的顶部,为什么会是未定义的?

你打错了,试试这个:

res.should.have.property('status', 201)

res.status.should.be.equal(201)

should.equal(res.status, 201)

或安装 should-http.