基于 this使用 chai 测试 angularjs 应用程序的教程,我想使用“应该”样式添加对未定义值的测试。这失败了:
it ('cannot play outside the board', function() {
scope.play(10).should.be.undefined;
});
出现错误“TypeError:无法读取未定义的‘should’属性”,但测试通过了“expect”风格:
it ('cannot play outside the board', function() {
chai.expect(scope.play(10)).to.be.undefined;
});
如何让它与“应该”一起工作?
最佳答案
这是 should 语法的缺点之一。它通过将 should 属性添加到所有对象来工作,但如果返回值或变量值未定义,则没有对象来保存该属性。
documentation给出了一些解决方法,例如:
var should = require('chai').should();
db.get(1234, function (err, doc) {
should.not.exist(err);
should.exist(doc);
doc.should.be.an('object');
});
关于javascript - Chai:如何使用 'should' 语法测试未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19209128/