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
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).
ToBeTruthy() This Boolean matcher is used in Jasmine to check whether the result is equal to true or false.
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.
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.
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
Also possible with:
expect(item).toMatch(/true|false/);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With