-
-
Save philpalmieri/d1ba2f082bae7b7c4202ef99c1233560 to your computer and use it in GitHub Desktop.
How to mock jQuery calls with Jasmine. Just some examples ;)
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
describe('Mock jQuery calls with Jasmine', function() { | |
it('how to mock $("test").show()', function() { | |
// mock the call | |
spyOn($.fn, 'show').andCallFake(function() { | |
return true; | |
}); | |
// now we can call the mock | |
result = $("test").show(); | |
// check result | |
expect(result).toBe(true); | |
// or, check directly; but: it will not be tested, if your parameter is "test" | |
expect($("test").show).toHaveBeenCalled(); | |
}); | |
it('how to mock $.cookie("test")', function() { | |
// mock the call | |
spyOn($, 'cookie').andCallFake(function() { | |
return true; | |
}); | |
// now we can call the mock | |
result = $.cookie("test"); | |
// check result | |
expect(result).toBe(true); | |
// or, check directly | |
expect($.cookie).toHaveBeenCalled(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment