Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 619
Vladimir Gordienko Avatar asked Jan 20 '14 14:01

Vladimir Gordienko


People also ask

What does ToBeTruthy mean?

toBeTruthy() is a comparison function that evaluates to true or false. It must be called in the chain after the t. expect(value) or . and(value).

What is use of ToBeTruthy?

ToBeTruthy() This Boolean matcher is used in Jasmine to check whether the result is equal to true or false.

How do I check if a variable is defined in Jasmine?

currentVal = 0; describe("Different Methods of Expect Block",function () { it("Example of toBeDefined", function () { expect(currentVal). toBeDefined(); }); }); In the above code, toBeDefined() will check whether the variable currentVal is defined in the system or not.

How does Jasmine define angular spec?

We use the Jasmine-provided it function to define a spec. The first parameter of it is a text description of what the spec will be testing — in this case we have a defined component. The second parameter is a function that will run the test. We then use Jasmine's expect function to define our expectation.


2 Answers

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

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

How to create a Custom Matcher

like image 62
Dalorzo Avatar answered Oct 04 '22 02:10

Dalorzo


Also possible with:

expect(item).toMatch(/true|false/);
like image 34
Julius Avatar answered Oct 04 '22 01:10

Julius