Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a value is an integer or a string in jasmine.js?

Tags:

jasmine

I am writing unit test using Jasmine in a web app using BackboneJS.
There are a lot of examples showing you how to check a value in this way:

        it("should set the id property to default value", function()         {             expect(this.task.get("id")).toEqual(null);         }); 

But I can't find any example checking if an attribute whether is number or string in Javascript using Jasmine.

Is it appropriate to make a check like this?
If yes, what is the proper way to make it?

Example: I want to check if the id is an integer > 0. How can I make it in Jasmine?

like image 392
js999 Avatar asked May 28 '12 09:05

js999


People also ask

How do you check if a value is an integer in JavaScript?

The Number. isInteger() method in JavaScript is used to check whether the value passed to it is an integer or not. It returns true if the passed value is an integer, otherwise, it returns false.

How do you check Jasmine strings?

stringMatching to check if a string has a given substring or pattern. For instance, we can write: describe('jasmine. stringMatching', function () { it("matches as a regex", function () { expect({ foo: 'baz' }) .

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.


2 Answers

For posterity, one of the questions posed here is to test whether a value is a number. From the jasmine docs:

expect(12).toEqual(jasmine.any(Number)); 
like image 96
Brett Avatar answered Oct 03 '22 23:10

Brett


I would make something like this:

    describe("when instantiated", function()      {         it("should exhibit attributes", function ()          {               .....             expect(this.task.get("id")).toMatch(/\d{1,}/);             .....         });     }); 
like image 21
antonjs Avatar answered Oct 03 '22 21:10

antonjs