Note that there are some explanatory texts on larger screens.

plurals
  1. POJasmine angularjs - spying on a method that is called when controller is initialized
    primarykey
    data
    text
    <p>I am currently using Jasmine with Karma(Testacular) and Web Storm to write unit test. I am having trouble spying on a method that gets called immediately when the controller is initialized. Is it possible to spy on a method that is called when the controller is initialized?</p> <p>My controller code, the method I am attempting to spy on is <code>getServicesNodeList()</code>.</p> <pre><code>myApp.controller('TreeViewController', function ($scope, $rootScope ,$document, DataServices) { $scope.treeCollection = DataServices.getServicesNodeList(); $rootScope.viewportHeight = ($document.height() - 100) + 'px'; }); </code></pre> <p>And here is the test spec:</p> <pre><code>describe("DataServices Controllers - ", function () { beforeEach(angular.mock.module('myApp')); describe("DataServicesTreeview Controller - ", function () { beforeEach(inject(function ($controller, $rootScope, $document, $httpBackend, DataServices) { scope = $rootScope.$new(), doc = $document, rootScope = $rootScope; dataServices = DataServices; $httpBackend.when('GET', '/scripts/internal/servicedata/services.json').respond(...); var controller = $controller('TreeViewController', {$scope: scope, $rootScope: rootScope, $document: doc, DataServices: dataServices }); $httpBackend.flush(); })); afterEach(inject(function($httpBackend){ $httpBackend.verifyNoOutstandingExpectation(); $httpBackend.verifyNoOutstandingRequest(); })); it('should ensure DataServices.getServicesNodeList() was called', inject(function ($httpBackend, DataServices) { spyOn(DataServices, "getServicesNodeList").andCallThrough(); $httpBackend.flush(); expect(DataServices.getServicesNodeList).toHaveBeenCalled(); })); }); }); </code></pre> <p>The test is failing saying that the method has not been called. I know that I should mock the <code>DataServices</code> and pass that into the test controller. But it seems like I would still have the same problem when spying on that method whether it is a mock or not. Anyone have any ideas or could point me to resources on the correct way to handle this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload