javascript - Chai:如何使用 'should' 语法测试未定义

标签 javascript angularjs testing chai

基于 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/

相关文章:

node.js - 我的 Karma 配置跳过了 Jasmine 单元测试

git - merge Hook ,用于在没有 merge 请求的情况下通过所有测试

php - Lithium PHP 集成测试 - 不包括 routes.php?

javascript - 使用jQuery按下回车键时单击链接

javascript - Angular JS : How to call a function inside link from controller in angular directive

javascript - 通过 socket.io Swift 客户端在 iOS 中使用 Socket.io

javascript - $rootScope.$emit 是否向所有 Controller (包括它自己)广播?

java - 公历 - 基于文档的测试

javascript - 将已编译的下划线模板附加到文档片段

javascript - div 行下方的空格