GithubHelp home page GithubHelp logo

chai-bookshelf's Introduction

chai-bookshelf

Make assertions on your bookshelf.js models.

Build Status

Install

Install via npm: npm install chai-bookshelf

Note that it's handy to use the shortcut --save-dev: npm install --save-dev chai-bookshelf

Using

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

Assertions

Right now, only basic assertions on relationships are supported.

Relationships

Remove boiler plate from your code by making straightforward assertions

Supported Relationships include:

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

Basic Example

A basic example showing a test of a relationship

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

Specifying the attribute name example

By default, the assertion will use the singular form of the tablename. If the attribute is named something other than the target model's class (for example, to be plural) you will need to specify the name of the attribute that represents the relationship.

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

chai-bookshelf's People

Contributors

elliotf avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

keithamus

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.