mardi 1 septembre 2015

injections issues in unit testing using karma angularjs

I need to test a controller with a lot of injections (services, factories, values), now I have a problem with a value...

I have the following situation:

This is my controller.

(function () {
'use strict';
angular
    .module('otpConfigureDatasets')
    .controller('otpConfigureDatasetsController', otpConfigureDatasetsController);

otpConfigureDatasetsController.$inject = ['$location', '$state', 'otpWebMapApp', 'otpWMDeltaTracker', 'otpWMStateCache', '$scope', '$timeout', 'otpConfigureDatasetService', 'otpAllDatasetsService', 'otpReviewListDatasetsService','otpConfigureDatasetSelectedTab'];

function otpConfigureDatasetsController($location, $state, otpWebMapApp, otpWMDeltaTracker, otpWMStateCache, $scope, $timeout, otpConfigureDatasetService, otpAllDatasetsService, otpReviewListDatasetsService, otpConfigureDatasetSelectedTab) {

    var vm = this;
    ...

    vm.tabs = [{
        title: 'MY REVIEW LIST',
        selectedAmt: vm.reviewData,
        contentTemplate: './app/features/configureDatasets/reviewListDatasets.html',
        active: otpConfigureDatasetSelectedTab.tab == 'MyReviewList'
    }, {
        title: 'ALL DATASETS',
        selectedAmt: vm.allData,
        contentTemplate: './app/features/configureDatasets/allDatasets.html',
        active: otpConfigureDatasetSelectedTab.tab == 'AllDatasets'
    }];

    ...
}
})();

My Spec.js below

'use strict';

describe('Test', function () {

var MainCtrl, scope, _otpWebMapApp_, _otpWMDeltaTracker_, _otpWMStateCache_, _otpConfigureDatasetService_,
    _otpAllDatasetsService_, _otpReviewListDatasetsService_, _otpConfigureDatasetSelectedTab_;


beforeEach(function () {
    module('otpConfigureDatasets');
});


beforeEach(inject(function ($controller, $rootScope) {

    scope = $rootScope.$new();
    MainCtrl = $controller('otpConfigureDatasetsController', {
        $scope: scope, otpWebMapApp: _otpWebMapApp_, otpWMDeltaTracker: _otpWMDeltaTracker_, otpWMStateCache: _otpWMStateCache_,
        otpConfigureDatasetService: _otpConfigureDatasetService_, otpAllDatasetsService: _otpAllDatasetsService_,
        otpReviewListDatasetsService: _otpReviewListDatasetsService_, otpConfigureDatasetSelectedTab: _otpConfigureDatasetSelectedTab_
    });    

}));



it('Should be true', function () {
    expect(true).toBe(true);
});


});

But when I try to run the test case, It doesn´t work and display the following error:

INFO [karma]: Karma v0.12.37 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Windows 8 0.0.0)]: Connected on socket NHknSeC3p_h_lf12SK Gh with id 27552356
PhantomJS 1.9.8 (Windows 8 0.0.0) Test Should be true FAILED
    TypeError: 'undefined' is not an object (evaluating 'otpConfigureDatasetSelectedTab.tab')
        at otpConfigureDatasetsController (D:/workspace/.....

I already have all the required files in the karma.config.js....

If I comment these two lines in the controller.

//active: otpConfigureDatasetSelectedTab.tab == 'MyReviewList'

and

//active: otpConfigureDatasetSelectedTab.tab == 'AllDatasets'

The unit testing works fine.

What´s wrong in my Spec definition???

Aucun commentaire:

Enregistrer un commentaire