Created
October 21, 2012 01:14
-
-
Save adamyanalunas/3925377 to your computer and use it in GitHub Desktop.
jasmine.js matcher to test type of object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jasmine.Matchers.prototype.toBeTypeOf = function(expected) { | |
var actual, notText, objType; | |
actual = this.actual; | |
notText = this.isNot ? 'not ' : ''; | |
objType = actual ? Object.prototype.toString.call(actual) : ''; | |
this.message = function() { | |
return 'Expected ' + actual + notText + ' to be an array'; | |
} | |
return objType.toLowerCase() === '[object ' + expected.toLowerCase() + ']'; | |
} |
Worth noting, that in Jasmine 2.0 you can use jasmine.any()
matcher:
expect(12).toEqual(jasmine.any(Number))
as mentioned here
+1 for @FilipZawada's suggestion of jasmine#any
I like the idea.
Saves you the trouble of writing e.g. expect(typeof(var)).toEqual('number')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the idea! Shouldn't the space in
notText
be on the other side to separate theactual
value and not do double the space in front of the following text at the line 9? i fixed it im my fork and added toBeInstanceOf to check other than primitive types.