daytonn
7/1/2012 - 3:23 AM

Custom Jasmine Matchers

Custom Jasmine Matchers

beforeEach(function() {
  this.addMatchers({
    toBeTrue: function() {
      return this.actual === true;
    },

    toBeFalse: function() {
      return this.actual === false;
    },

    toBeObject: function() {
      if (typeof this.actual === "undefined") {
        return false;
      }
      else {
        return compareConstructor(this.actual.constructor, Object);
      }
    },

    toBeArray: function() {
      return compareConstructor(this.actual.constructor, Array);
    },

    toBeString: function() {
      return compareConstructor(this.actual.constructor, String);
    },

    toBeFunction: function() {
      return compareConstructor(this.actual, Function);
    },

    toBeTypeof: function(b) {
      return compareConstructor(this.actual, b);
    },

    toBeEmpty: function() {
      return this.actual.length === 0;
    }
  });

  function compareConstructor(a, b) {
    if (typeof a === "undefined") {
      return false;
    }
    else {
      return a.constructor == b;
    }
  }

});