Reputation: 7317
The following jasmine-jquery snippet fails. The error I get is "Expected spy logIt to have been called".
var logIt = function () {
console.log("logged");
};
$('#id1').on({
click: logIt
});
describe("Clicking id1", function() {
it("logs to the console.", function() {
spyOn(window, 'logIt');
$('#id1').click();
expect(window.logIt).toHaveBeenCalled();
});
});
//ERROR: "Expected spy logIt to have been called."
Upvotes: 0
Views: 408
Reputation: 3149
Try this:
describe("Clicking id1", function() {
var logIt, $button;
beforeAll(function () {
logIt = jasmine.createSpy('logIt() spy');
$button = $('#id1');
$button.click(logIt);
$button.click();
});
it("logs to the console.", function () {
expect(logIt).toHaveBeenCalled();
});
});
I ran the test below in my project and it worked:
describe("Clicking id1", function() {
var $button, specHelper, logIt;
beforeAll(function () {
logIt = jasmine.createSpy('logIt() spy');
$button = $('<button>').attr("id", "id1");
specHelper = new SpecHelper();
specHelper.publish($button);
$button.click(logIt);
$button.click();
});
afterAll(function () {
specHelper.conceal($button);
});
it("logs to the console.", function () {
console.dir(logIt);
expect(logIt).toHaveBeenCalled();
});
});
specHelper.publish
appends the jQuery node to the DOM and specHelper.conceal
removes it.
Upvotes: 1