I am testing mechanism that should call method once and prevent any subsequent calls with jasmine, I can see by attaching breakpoints that method is NOT being called second time however jasmine test fails. I would assume it has to do with spy
not being designed to be used for multiple checks.
What would be proper solution to given situation?
JSfiddle of Code that is being tested I could not figure out how to do jasmine test jsfiddle properly (Jasmine version I am using is 1.3.1 while test template is on 1.2.0).
Test looks like this:
it("Invoking OnPreQuery will add event listener for OnTheMoveViewPreLoad event. Triggering OnTheMoveViewPreLoad twice will call getChildrenForMasterRecordList only first time", function () { AddTreeSettingsObjectToBC({ bc: bc, Tree: { IncludeChildren: true} }); ComposeMockPageObjWithObservableFieldsWithChildren(); var preQuerySpy = spyOnEvent(onTheMove.PageDataRoles, 'OnPreQuery'); $(onTheMove.PageDataRoles).trigger('OnPreQuery', { knockoutContextName: 'bc' }); expect('OnPreQuery').toHaveBeenTriggeredOn(onTheMove.PageDataRoles); expect(preQuerySpy).toHaveBeenTriggered(); var getChildrenForMasterRecordListSpy = spyOn(window, 'getChildrenForMasterRecordList'); $(onTheMove.PageDataRoles).trigger('OnTheMoveViewPreLoad', { knockoutContextName: 'bc' }); expect(getChildrenForMasterRecordListSpy).toHaveBeenCalled(); $(onTheMove.PageDataRoles).trigger('OnTheMoveViewPreLoad', { knockoutContextName: 'bc' }); expect(getChildrenForMasterRecordListSpy).not.toHaveBeenCalled(); });
Code that is being tested: HTML
<div data-role="page"></div>
Javascript
var onTheMove = function(){}; $.extend(onTheMove,{ NullValue : "null", PageDataRoles : 'div[data-role="page"], div[data-role="dialog"]', OnTheMovePrefix : 'OnTheMove_' }); $(document).on('OnPreQuery', onTheMove.PageDataRoles, function (e, options) { var isChildAttachmentQueued = true; var knockoutContextName = options.knockoutContextName; if (TreeEnabled(knockoutContextName)) { var isModelReadyToAttachChildren = function () { var isReady = false; if (PageObj[knockoutContextName] != undefined) { isReady = (PageObj[knockoutContextName]().length > 0) && isChildAttachmentQueued; } return isReady; }; var treeSettings = eval(knockoutContextName).Tree; treeSettings.knockoutContextName = knockoutContextName; $(onTheMove.PageDataRoles).on('OnTheMoveViewPreLoad', function (e, options) { if (isModelReadyToAttachChildren()) { getChildrenForMasterRecordList({ parentTable: eval(knockoutContextName).primaryTableName, knockoutContextName: treeSettings.knockoutContextName, parentIdColumn: treeSettings.ParentIdColumn, masterIdColumn: treeSettings.MasterIdColumn }); isChildAttachmentQueued = false; } }); } }); function getChildrenForMasterRecordList(options) { console.log('beep'); }
getFullName() } getFullName() { this. firstName + this. lastName } } describe('Test Person', () => { beforeEach(() => { const programmer = new Person('John', 'Gray'); spyOn(programmer, 'getFullName'); programmer. getName(); }); it('getName should call getFullName', () => { expect(programmer.
Jasmine doesn't actually support parallel execution. It runs one spec (or before/after) function at a time.
spyOn() spyOn() is inbuilt into the Jasmine library which allows you to spy on a definite piece of code.
SpyOn is a Jasmine feature that allows dynamically intercepting the calls to a function and change its result.
Figured it out myself, spy has property callCount
that auto-increments by one on each call.
it("Invoking OnPreQuery will add event listener for OnTheMoveViewPreLoad event. Triggering OnTheMoveViewPreLoad twice will call getChildrenForMasterRecordList only first time", function () { AddTreeSettingsObjectToBC({ bc: bc, Tree: { IncludeChildren: true} }); ComposeMockPageObjWithObservableFieldsWithChildren(); var preQuerySpy = spyOnEvent(onTheMove.PageDataRoles, 'OnPreQuery'); $(onTheMove.PageDataRoles).trigger('OnPreQuery', { knockoutContextName: 'bc' }); expect('OnPreQuery').toHaveBeenTriggeredOn(onTheMove.PageDataRoles); expect(preQuerySpy).toHaveBeenTriggered(); var getChildrenForMasterRecordListSpy = spyOn(window, 'getChildrenForMasterRecordList'); $(onTheMove.PageDataRoles).trigger('OnTheMoveViewPreLoad', { knockoutContextName: 'bc' }); expect(getChildrenForMasterRecordListSpy).toHaveBeenCalled(); $(onTheMove.PageDataRoles).trigger('OnTheMoveViewPreLoad', { knockoutContextName: 'bc' }); expect(getChildrenForMasterRecordListSpy.callCount).toEqual(1); });
as per comment
in Jasmine 2.0 its
expect(object.func.calls.count()).toBe(1);
toHaveBeenCalledTimes(1)
now makes this much easier.
expect(yourSpy).toHaveBeenCalledTimes(1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With