chai-bookshelf

bookshelf.js モデルにアサーションを作成します。

Build Status

インストール

npm でインストール: npm install chai-bookshelf

ショートカット –save-dev を使用すると便利です: npm install --save-dev chai-bookshelf

使用

var chai = require('chai');
chai.use(require('chai-bookshelf'));

アサーション

現時点では、リレーションシップに対する基本的なアサーションのみがサポートされています。

リレーションシップ

単純なアサーションを作成することで、コードからボイラープレートを削除します

サポートされるリレーションシップ

  • hasOne expect(ClassA).to.haveOne(ClassB)
  • hasMany expect(ClassA).to.haveMany(ClassB)
  • belongsTo expect(ClassA).to.belongTo(ClassB)
  • belongsToMany expect(ClassA).to.belongToMany(ClassB)

基本的な例

リレーションシップのテストを示す基本的な例

describe('User model', function() {
  var User
    , Thing
  ;

  beforeEach(function() {
    Thing = db.Model.extend({
      tableName: 'things'
    });

    User = db.Model.extend({
      things: function() {
        return this.hasMany(Thing);
      }
    });
  });

  describe('Relationships', function() {
    it('has many things', function() {
      expect(User).to.haveMany(Thing);
    })
  });
});

属性名の指定の例

既定では、アサーションではテーブル名の単数形が使用されます。属性が、ターゲットモデルのクラス以外の何かに命名されている場合(例: 複数形にする場合)は、リレーションシップを表す属性の名前を指定する必要があります。

describe('User model', function() {
  var User
    , Thing
  ;

  beforeEach(function() {
    User = db.Model.extend({});

    Thing = db.Model.extend({
      owner: function() {
        this.belongsTo(User);
      }
    });
  });

  describe('Relationships', function() {
    it('has many things', function() {
      expect(Thing).to.belongTo(User, 'owner');
    })
  });
});