Skip to content

Instantly share code, notes, and snippets.

@philpalmieri
Forked from quitschibo/testMocks.js
Created January 25, 2018 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philpalmieri/d1ba2f082bae7b7c4202ef99c1233560 to your computer and use it in GitHub Desktop.
Save philpalmieri/d1ba2f082bae7b7c4202ef99c1233560 to your computer and use it in GitHub Desktop.
How to mock jQuery calls with Jasmine. Just some examples ;)
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