Vladimir Gordienko Vladimir Gordienko - 6 months ago 79
AngularJS Question

Jasmine - any boolean (jasmine.any(Boolean))

I write unit tests for angular using karma, jasmine. Try to write:

expect(item).toEqual(jasmine.any(Boolean));


but got:

Expected true to equal <jasmine.any(function Boolean() { [native code] })>.


mm.. maybe i do something wrong) or is it another way to write test for value in that case:

if (true or false) - passed, if any other - fail

Answer

I think what you need is a custom Matcher something like this:

toBeBoolean : function () {
  return {
    compare : function (actual, expected) {
      return {
        pass : (typeof expected === 'boolean'),
        message : 'Expected ' + actual + ' is not boolean'
      };
    }
  };
}

How to create a Custom Matcher

Comments