LoginSignup
3
1

More than 5 years have passed since last update.

should.js で null や undefined をテストする

Last updated at Posted at 2017-07-18

アサーションライブラリ sholud.js は、JavaScript の Object クラスを拡張して should プロパティを加える。これにより、次のように英語として読める表記を実現している。

   var foo = {name:'abc'};
   foo.should.have.property('name', 'abc');

だがこれは foo が nullundefined の場合にうまく動作しない。

   var foo = null;
   foo.should.null();

   TypeError: Cannot read property 'should' of null

同様に undefined の場合では TypeError: Cannot read property 'should' of undefined となる。

このようなケースを扱うには should を関数として使う。

   var should = require('should');
   var foo = null;
   should(foo).null();

参考: should API Reference覚書
chai.should を使う方はこちら: mocha + chai.should で null や undefined をテストする

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1