'How to check if a value is an integer or a string in jasmine.js?
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?
Solution 1:[1]
I would make something like this:
describe("when instantiated", function()
{
it("should exhibit attributes", function ()
{
.....
expect(this.task.get("id")).toMatch(/\d{1,}/);
.....
});
});
Solution 2:[2]
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));
Solution 3:[3]
Honestly, I don't know what the proper way is, but I will write something like that:
it("should set the id property to default value", function () {
var id = this.task.get("id");
expect(typeof id).toEqual('number');
expect(id).toBeGreaterThan(0);
});
Solution 4:[4]
expect( this.task.get("id") ).toBeGreaterThan( 0 );
If we have in consideration that:
expect( 1 ).toBeGreaterThan( 0 ); // => true
expect( "1" ).toBeGreaterThan( 0 ); // => true
expect( "a" ).toBeGreaterThan( 0 ); // => false
Solution 5:[5]
You can try:
it('should be integer', function()
{
var id = this.task.get("id");
expect(id).toEqual(jasmine.any(Number));
expect(id).toBeGreaterThan(0);
expect(Math.trunc(id)).toEqual(id);
});
If you have a number that is not an integer, truncating it should lead to a number that is different, which will cause the appropriate test failure.
If you do not have support for ES6, you can use floor instead.
Solution 6:[6]
I've used underscorejs to check this kind of things:
it('should be a number', function() {
expect(_.isNumber(this.task.get('id'))).toBeTruthy();
});
Solution 7:[7]
This will make sure that a result is a number.
it("expect the result to be a number", () => {
expect(5).not.toBeNaN;
});
While this will make sure the result not to be a number
it("expect the result not to be a number", () => {
expect('five').toBeNaN;
});
This makes sure that the number is greater than or equal to zero
it("expect the result to be >=0", () => {
expect(5).toBeGreaterThanOrEqual(0)
});
Solution 8:[8]
expect(id).toBeInstanceOf(Number)
expect(id).toBeGreaterThan(0);
https://jasmine.github.io/api/edge/matchers.html#toBeInstanceOf
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | antonjs |
| Solution 2 | Diogo Cardoso |
| Solution 3 | theotheo |
| Solution 4 | fguillen |
| Solution 5 | |
| Solution 6 | Michael Radionov |
| Solution 7 | Peter Csala |
| Solution 8 | jobwat |
