.toHaveBeenCalled() true when should be false

253 Views Asked by At

When I run the following test, it passes. It is supposed to fail (note the commented line making the tap() call). It is only supposed to pass when the tap() call is made. Would anyone have any idea why $.mobile.loading is showing as having been called when I don't send a tap() event?

beforeEach(function() {
jasmine.getFixtures().fixturesPath = ".";
loadFixtures('index.html');  
});

/* this test is buggy - it passes even when the tap() method is not called */
it("should display a PageLoadingMsg", function() {
var PageLoadingMsgCount = 0;

runs(function() {
  $('#account_creation').trigger('pageinit');
  console.log('d');
  spyOn($.mobile, 'loading').andCallFake(function() {
    PageLoadingMsgCount++;
    console.log('a');
  });
  console.log('e');
  spyOn($.mobile, 'hidePageLoadingMsg');
  spyOn($.mobile, 'changePage');
});

runs(function() {
  expect($('#account_creation_create_account_button')).toHaveAttr('data-role','button');
  // $('#account_creation_create_account_button').tap();
});

waitsFor(function() {
  return (PageLoadingMsgCount > 0);
}, "PageLoadingMsgShown timed out", 1000);

runs(function() {
  expect($.mobile.loading).toHaveBeenCalled();
  console.log(PageLoadingMsgCount);
  expect(PageLoadingMsgCount).toBeGreaterThan(0);
});
});

Here is the call it is trying to spy on:

$(document).on('pageinit', '#account_creation', function(event) {

$(document).on('tap', '#account_creation_create_account_button', function(event) {
    event.stopImmediatePropagation();
    event.preventDefault();

    console.log('b');
    $.mobile.loading('show', {theme:settings['theme'], text:"Creating account..."});
    console.log('c');
    ...

Thanks, David

1

There are 1 best solutions below

1
On

I just ended up waiting for the call count to get to 2.

waitsFor(function() {
  return (PageLoadingMsgCount > 1);
}, "PageLoadingMsgShown timed out", 1000);