kshitij
kshitij

Reputation: 624

Uncaught TypeError: Cannot read property 'service.method' of undefined

describe('Controller:insightSettingsController', function() {
'use strict';
var insightSettingsCtrl;
var settingsService;
var UtilsService;
var scope;
var updateMethodDeferred;
var httpBackend;
var deferred;
var q;
var spy;
beforeEach(module('ui.router',proname.components.insightSettings.module.name));

beforeEach(function() {
var mockUtilsService = {};
module(function(_$provide_) {
  _$provide_.value('UtilsService', mockUtilsService);
});

inject(function($q) {
  deferred = $q.defer();
  mockUtilsService.getConfigurations = function() {};
  spyOn(mockUtilsService, 'getConfigurations')
      .and.returnValue(deferred.promise);
});

});
beforeEach(inject(function(
  _$controller_, _$httpBackend_, _$rootScope_, _settingsService_,
  _UtilsService_, _$q_) {
  scope = _$rootScope_.$new();
  spy = jasmine.createSpy();
  settingsService = _settingsService_;
  UtilsService = _UtilsService_;
  httpBackend = _$httpBackend_;
  insightSettingsCtrl = function() {
  return _$controller_(
      dbmcm.components.settings.insightSettingsController,
      {$scope: scope, UtilsService: UtilsService});
};

}));

describe(
'Check Existing Function , parameters, Initialization of function',
function() {
it('should call UtilsService.getConfigurations() once',
  function() {
    insightSettingsCtrl();
    deferred.resolve();
    scope.$digest();
    expect(UtilsService.getConfigurations).toHaveBeenCalled();
  });
});

});

Hi, I am writing angular test cases for my separate module.Unfortunately, my module above breaks down my test cases ,getting error debug.js:44 Uncaught Type Error: Cannot read property 'get Configurations' of undefined. I am firing getConfigurations function at the time of initialization of controller please have a look below

 constructor: function(UtilsService) {
 UtilsService.getConfigurations().then(function(response) {
   this.utilsConfig_ = response;
 }.bind(this));
}

I think Jasmine's spy only checks if the function has been called, without firing the actual implementation. That's why getConfigurations().then throws an error. for this I applied .and.callThrough() in my test cases

it('should call UtilsService.getConfigurations() once',
  function() {
   spyOn(UtilsService, 'getConfigurations').and.callThrough();
    insightSettingsCtrl();
    deferred.resolve();
    scope.$digest();
    expect(UtilsService.getConfigurations).toHaveBeenCalled();
  });
});

After that I am getting debug.js:44 Uncaught Error: getConfigurations has already been spied upon

Please correct me where I am doing wrong. Thanks in advance

Upvotes: 1

Views: 1021

Answers (1)

RIYAJ KHAN
RIYAJ KHAN

Reputation: 15292

Either you mock it or inject it if possible.

You are doing both for UtilsService.

I am assuming you injecting UtilsService.

So,remove the following code :

module(function(_$provide_) {
  _$provide_.value('UtilsService', mockUtilsService);
});

inject(function($q) {
  deferred = $q.defer();
  mockUtilsService.getConfigurations = function() {};
  spyOn(mockUtilsService, 'getConfigurations')
      .and.returnValue(deferred.promise);
})

Upvotes: 1

Related Questions