Created
September 4, 2012 19:45
-
-
Save heat/3625555 to your computer and use it in GitHub Desktop.
Jasmine Matcher instanceof
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
beforeEach(function() { | |
this.addMatchers({ | |
toBeInstanceOf: function(expectedInstance) { | |
var actual = this.actual; | |
var notText = this.isNot ? " not" : ""; | |
this.message = function() { | |
return "Expected " + actual.constructor.name + notText + " is instance of " + expectedInstance.name; | |
}; | |
return actual instanceof expectedInstance; | |
} | |
}); | |
}); |
Thank you! And note that jasmine.Any
is different from jasmine.any
or simply:
expect(myVariable instanceof Array).toBeTruthy();
@grebett: Problem with that approach is the error message which is completely unhelpful. With jasmine.any
you'll get a dump of what was actually passed in to the matcher:
Expected '12' to equal <jasmine.any(Number)>.
Instead of the much less helpful:
Expected false to be true.
I tough you could set up the output to the expectation as the second arg to .toEqual()
, .toBeTruthy()
and most expectation comparison does accept some kind of configuration argument.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you @FilipZawada