1. ホーム
  2. javascript

[解決済み] Chai: 'should'構文で未定義をテストする方法

2023-01-12 14:52:02

質問

建物 この tutorial test an angularjs app with chai, I want to add a test for an undefined value using the "should" style. これは失敗します。

it ('cannot play outside the board', function() {
  scope.play(10).should.be.undefined;
});

というエラーで、"TypeError: Cannot read property 'should' of undefined" と表示されますが、テストは "expect" のスタイルでパスします。

it ('cannot play outside the board', function() {
  chai.expect(scope.play(10)).to.be.undefined;
});

どうすれば"should"で動作させることができるのでしょうか?

どのように解決するのですか?

これはshould構文の欠点の1つです。すべてのオブジェクトにshouldプロパティを追加することで動作しますが、戻り値や変数値が未定義の場合、プロパティを保持するオブジェクトがありません。

また ドキュメント は、例えばいくつかの回避策を提供しています。

var should = require('chai').should();
db.get(1234, function (err, doc) {
  should.not.exist(err);
  should.exist(doc);
  doc.should.be.an('object');
});