Skip to content

Instantly share code, notes, and snippets.

@mich-cook
Created October 3, 2011 22:07
Show Gist options
  • Save mich-cook/1260389 to your computer and use it in GitHub Desktop.
Save mich-cook/1260389 to your computer and use it in GitHub Desktop.
The spy missed its chance to report a call.
(function() {
var MYAPP = {};
window = jsdom().createWindow(),
$ = require('jquery').create(window),
document = window.document,
jQuery = $j= $;
$('body').html('<div class="click-me"></div>');
MYAPP.module = {
hello : function() {
console.log('hi there');
},
initListeners : function() {
$('.click-me').click(this.hello);
}
};
$(document).ready(function () {
MYAPP.module.initListeners();
});
describe('Does not seem to spy on functions correctly', function() {
// clumsy hack to get the DOM to fire its ready event
// for some reason it won't do this before we start to
// do things that need it to be done.
it('needs a kick in the butt to get the DOM ready', function() {});
it('should see that there was a click that triggered a function to be called', function() {
spyOn(MYAPP.module, 'hello');
console.log('going to click now');
$('.click-me').trigger('click');
console.log('i just clicked');
console.log(MYAPP.module.hello);
expect(MYAPP.module.hello).toHaveBeenCalled();
});
});
})();
{ [Function]
identity: 'hello',
isSpy: true,
plan: [Function],
mostRecentCall: {},
argsForCall: [],
calls: [],
andCallThrough: [Function],
andReturn: [Function],
andThrow: [Function],
andCallFake: [Function],
reset: [Function],
wasCalled: false,
callCount: 0,
baseObj: { hello: [Circular], initListeners: [Function] },
methodName: 'hello',
originalValue: { [Function] guid: 1 } }
me:~/tests/jasmine$ ./runspecs.js
Started
.going to click now
hi there
i just clicked
F
Spec Does not spy on functions correctly
it needs a kick in the butt to get the DOM ready
it should see that there was a click that triggered a function to be called
Error: Expected spy hello to have been called.
at [object Object].<anonymous> (/home/me/tests/jasmine/specs/calledButNotCalled.spec.js:36:40)
Finished in 0.006 seconds
1 test, 1 assertion, 1 failure
me:~/tests/jasmine$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment