101

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;
});

「すべき」で動作させるにはどうすればよいですか?

4

9 に答える 9

18

未定義のテスト

var should = require('should');
...
should(scope.play(10)).be.undefined;

null のテスト

var should = require('should');
...
should(scope.play(10)).be.null;

偽のテスト、つまり、条件で偽として扱われる

var should = require('should');
...
should(scope.play(10)).not.be.ok;
于 2015-06-02T12:51:04.163 に答える
10

未定義のテスト用の should ステートメントを書くのに苦労しました。以下は動作しません。

target.should.be.undefined();

次の解決策を見つけました。

(target === undefined).should.be.true()

型チェックとして書くこともできます

(typeof target).should.be.equal('undefined');

上記の方法が正しいかどうかはわかりませんが、うまくいきます。

githubのゴーストからの投稿によると

于 2016-06-08T23:51:31.043 に答える
5

これを試して:

it ('cannot play outside the board', function() {
   expect(scope.play(10)).to.be.undefined; // undefined
   expect(scope.play(10)).to.not.be.undefined; // or not
});
于 2013-10-06T13:26:51.423 に答える
0

関数の結果をラップしてshould()、「未定義」のタイプをテストできます。

it ('cannot play outside the board', function() {
  should(scope.play(10)).be.type('undefined');
});
于 2014-02-20T21:46:36.420 に答える