Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine calls.length and callCount are undefined

I'm trying to test my chrome extension with Jasmine but I'm having trouble getting calls.length and callCount to behave as expected. Both cases return undefined.

I've included a sample of the code and the spec. Here's the rest of the code if it helps: https://github.com/DruRly/kamikaze/tree/closeIdleTab

How to reproduce:

  • git clone https://github.com/DruRly/kamikaze/tree/closeIdleTab
  • cd kamikaze
  • open SpecRunner.html

spec/kamikazeSpec.js

describe("kamikaze", function() {   describe("closeIdleTabs", function(){     it("calls closeIdleTab for each tab received", function(){       spyOn(kamikaze, 'closeIdleTab');        kamikaze.closeIdleTabs([1,2,3]);       expect(kamikaze.closeIdleTab.calls.length).toBe(3);     })   }) }) 

src/kamikaze.js

kamikaze = {   ...    closeIdleTabs: function(tabs){     tabs.forEach(function(tab){       test.closeIdleTab(tab);     })   },    closeIdleTab: function(tab){     if(tabTimeStamps[tab.id]){       var secondsSinceUpdated = getSecondsSinceUpdated(tab.id)       if(secondsSinceUpdated > (minutesUntilIdle * 60)){         chrome.tabs.remove(tab.id)       }     }   },    ... } 
like image 580
Dru Avatar asked Jan 07 '14 13:01

Dru


People also ask

How do you use spyOn in Jasmine?

Jasmine provides the spyOn() function for such purposes. spyOn() takes two parameters: the first parameter is the name of the object and the second parameter is the name of the method to be spied upon. It replaces the spied method with a stub, and does not actually execute the real method.

What does describe function defines in Jasmine?

The describe function is for grouping related specs, typically each test file has one at the top level. The string parameter is for naming the collection of specs, and will be concatenated with specs to make a spec's full name. This aids in finding specs in a large suite.

How do I change the return value of Jasmine spy?

beforeEach(inject(function ($injector) { $rootScope = $injector. get('$rootScope'); $state = $injector. get('$state'); $controller = $injector. get('$controller'); socket = new sockMock($rootScope); //this is the line of interest authService = jasmine.

How do you mock a function in Jasmine?

Using Jasmine spies to mock code You set the object and function you want to spy on, and that code won't be executed. In the code below, we have a MyApp module with a flag property and a setFlag() function exposed. We also have an instance of that module called myApp in the test. To spy on the myApp.


1 Answers

The Jasmine APIs have changed a bit in the 2.x version "series".
According to the latest docs you should use the count() method:

expect(kamikaze.closeIdleTab.calls.count()).toBe(3); 

I also tried that with your code and all tests pass successfully.

like image 50
gkalpak Avatar answered Oct 07 '22 22:10

gkalpak