【问题标题】:chai expect passing when returned value is undefined当返回值未定义时,柴期望通过
【发布时间】:2017-06-08 20:33:10
【问题描述】:

在我看来,这个测试不应该通过

柴测试

const chai = require('chai')
const expect = chai.expect;
const chaiHttp = require('chai-http');
const request = chaiHttp.expect;
const config = require('../../app/config');
const worker = require('../../app/worker');

chai.use(chaiHttp);

describe('server response', function () {
    .....

    it('should return 200', function () {
        chai.request(`http://localhost:${config.port}`)
        .get('/')
        .end(function (err, res) {
            expect('as').to.have.status(200);
        });
    });
});

终端

> NODE_ENV=test mocha --timeout 20000 --recursive test/ --compilers js:babel-core/register



  server response
    ✓ should return 200


  1 passing (150ms)

【问题讨论】:

  • 不应该通过因为?
  • 'as' 显然没有 200 的雕像 :)

标签: javascript node.js chai


【解决方案1】:

我认为您缺少 done 回调。像这样试试

it('should return 200', function (done) {
    chai.request(`http://localhost:${config.port}`)
    .get('/')
    .end(function (err, res) {
        expect('as').to.have.status(200);
        done();
    });
});

使用此代码,测试只有在以下情况下才能通过

expect('as').to.have.status(200);

是真的(在你当前的例子中总是假的)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-07
    • 2012-11-29
    • 1970-01-01
    • 2011-08-12
    相关资源
    最近更新 更多