mercredi 30 mars 2016

Unit testing in AngularJS with Jasmine and Chutzpah

I am trying to write Unit test cases for existing SPA project built on angularJS. I get the "Can't find variable: module" error whenever I try to execute the code.

I installed the libraries using npm.

I used Chutzpah and Jasmine libraries for this.

appController.js:

(function () {
'use strict';

  angular
      .module('app', [])
      .controller('appController', appController);

  appController.$inject = ['apiServices', '$scope'];

  function appController(apiServices, $scope) {
    $scope.value = 5;
  }
})();

apiServices.js

(function () {
'use strict';

  angular
      .module('app',[])
      .factory('apiServices', apiServices);

  apiServices.$inject = ['$http', '$log', '$q'];

  function apiServices($http, $log, $q) {
    var clientServicesPath = '/api/ClientServices',
    service =
       {  .......  };

    return service;
  }
})();

appControllerSpec.js

/// <reference path="../../../lib/angular/angular.js" />
/// <reference path="../../services/apiservices.js" />
/// <reference path="../../controllers/appcontroller.js" />
/// <reference path="../../../../node_modules/jasmine/bin/jasmine.js" />
/// <reference path="../../../../node_modules/jasmine/lib/jasmine.js" />
/// <reference path="../../../../node_modules/jasmine-ajax/lib/mock-ajax.js" />
/// <reference path="../../../lib/angular-mocks/angular-mocks.js" />

describe('When using appController ', function () {
  //initialize Angular
  beforeEach(module('app'));   
  var ctrl, scope, apiServices;

  beforeEach(inject(function ($injector) {
    apiServices = $injector.get('apiServices');
  }));

  beforeEach(inject(function ($controller, $rootScope, apiServices) {
    scope = $rootScope.$new();
    var ctrl = $controller('appController', { $scope: scope, apiServices: apiServices });
  }));

  it('initial value is 5', function () {
    expect(scope.value).toBe(5);
  });
});

I always get the following error:

Test 'When using appController :initial value is 5' failed Error: [$injector:unpr] Unknown provider: apiServicesProvider <- apiServices http://ift.tt/1Tj7CdW$injector/unpr?p0=apiServicesProvider%20%3C-%20apiServices (line 4418) at getService (file:///C:/Users/Bhanu/......./lib/angular/angular.js:4571:46) at file:///C:/Users/Bhanu/......./lib/angular/angular.js:4423:48 at getService (file:///C:/Users/Bhanu/......./lib/angular/angular.js:4571:46) at file:///C:/Users/Bhanu/......./js/TestingJS/controllers/appControllerSpec.js:16:36 at invoke (file:///C:/Users/Bhanu/......./lib/angular/angular.js:4625:24) Error: [$injector:unpr] Unknown provider: apiServicesProvider <- apiServices http://ift.tt/1Tj7CdW$injector/unpr?p0=apiServicesProvider%20%3C-%20apiServices (line 4418) at getService (file:///C:/Users/Bhanu/......./lib/angular/angular.js:4571:46) at file:///C:/Users/Bhanu/......./lib/angular/angular.js:4423:48 at getService (file:///C:/Users/Bhanu/......./lib/angular/angular.js:4571:46) at injectionArgs (file:///C:/Users/Bhanu/......./lib/angular/angular.js:4595:68) at invoke (file:///C:/Users/Bhanu/......./lib/angular/angular.js:4617:31) TypeError: undefined is not an object (evaluating 'scope.value') in file:///C:/Users/Bhanu/......./js/TestingJS/controllers/appControllerSpec.js (line 25) at attemptSync (file:///C:/Users/Bhanu/AppData/Local/Microsoft/VisualStudio/14.0/Extensions/3nmdcohr.lvt/TestFiles/jasmine/v2/jasmine.js:1886:28) at run (file:///C:/Users/Bhanu/AppData/Local/Microsoft/VisualStudio/14.0/Extensions/3nmdcohr.lvt/TestFiles/jasmine/v2/jasmine.js:1874:20) at execute (file:///C:/Users/Bhanu/AppData/Local/Microsoft/VisualStudio/14.0/Extensions/3nmdcohr.lvt/TestFiles/jasmine/v2/jasmine.js:1859:13) at queueRunnerFactory (file:///C:/Users/Bhanu/AppData/Local/Microsoft/VisualStudio/14.0/Extensions/3nmdcohr.lvt/TestFiles/jasmine/v2/jasmine.js:697:42) at execute (file:///C:/Users/Bhanu/AppData/Local/Microsoft/VisualStudio/14.0/Extensions/3nmdcohr.lvt/TestFiles/jasmine/v2/jasmine.js:359:28) in C:\Users\Bhanu.......\js\TestingJS\controllers\appControllerSpec.js (line 24)

I have tried all the possible solutions but none worked for me. I ran the tests directly by right clicking the Test controller file and selecting the option "Run JS Tests".

It works fine if I remove the service injection both from controller and test.

I feel there are more pieces towards configuration. Please help me with this.

Aucun commentaire:

Enregistrer un commentaire