Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine expect logic (expect A OR B)

I need to set the test to succeed if one of the two expectations is met:

expect(mySpy.mostRecentCall.args[0]).toEqual(jasmine.any(Number)); expect(mySpy.mostRecentCall.args[0]).toEqual(false); 

I expected it to look like this:

expect(mySpy.mostRecentCall.args[0]).toEqual(jasmine.any(Number)).or.toEqual(false); 

Is there anything I missed in the docs or do I have to write my own matcher?

like image 577
naugtur Avatar asked Nov 23 '12 13:11

naugtur


People also ask

What is expect in Jasmine?

An expectation in Jasmine is an assertion that is either true or false. A spec with all true expectations is a passing spec. A spec with one or more false expectations is a failing spec. describe("A suite", function() { it("contains spec with an expectation", function() { expect(true). toBe(true); }); });

What are matchers in Jasmine?

Jasmine is a testing framework, hence it always aims to compare the result of the JavaScript file or function with the expected result. Matcher works similarly in Jasmine framework. Matchers are the JavaScript function that does a Boolean comparison between an actual output and an expected output.

Which of the following Jasmine matchers can be used for matching a regular expression?

It checks whether something is matched for a regular expression. You can use the toMatch matcher to test search patterns.

What is describe and it in Jasmine framework?

Suite Block. Jasmine is a testing framework for JavaScript. Suite is the basic building block of Jasmine framework. The collection of similar type test cases written for a specific file or function is known as one suite. It contains two other blocks, one is “Describe()” and another one is “It()”.


2 Answers

Add multiple comparable strings into an array and then compare. Reverse the order of comparison.

expect(["New", "In Progress"]).toContain(Status); 
like image 158
Zs Felber Avatar answered Sep 20 '22 15:09

Zs Felber


This is an old question, but in case anyone is still looking I have another answer.

How about building the logical OR expression and just expecting that? Like this:

var argIsANumber = !isNaN(mySpy.mostRecentCall.args[0]); var argIsBooleanFalse = (mySpy.mostRecentCall.args[0] === false);  expect( argIsANumber || argIsBooleanFalse ).toBe(true); 

This way, you can explicitly test/expect the OR condition, and you just need to use Jasmine to test for a Boolean match/mismatch. Will work in Jasmine 1 or Jasmine 2 :)

like image 33
RoboBear Avatar answered Sep 18 '22 15:09

RoboBear