2015-04-30 5 views
8

Provare a impostare i test di unità per https://github.com/beeman/loopback-angular-admin.Angular/Karma/Jasmine: TypeError: 'undefined' non è un oggetto (valutando 'scope.awesomeThings')

app/modules/su/Controller/about.controller.js (ho aggiunto $scope.awesomeThings per caricare il campo con qualcosa da testare):

'use strict'; 
angular.module('com.module.about') 
    /** 
    * @ngdoc function 
    * @name com.module.about.controller:AboutCtrl 
    * @description 
    * # AboutCtrl 
    * Controller of the clientApp 
    */ 
    .controller('AboutCtrl', function($scope) { 
    $scope.angular = angular; 
    $scope.awesomeThings = [1, 2]; 
    }); 

La prova di gelsomino al client/test/modules/su/controllori/about.ctrl.js

'use strict'; 

describe('Controller: AboutCtrl', function() { 
    var AboutCtrl, 
    scope; 

    // load the controller's module 
    beforeEach(module('gettext')); 
    beforeEach(module('ui.router')); 
    beforeEach(module('com.module.about')); 

    // Initialize the controller and a mock scope 
    beforeEach(inject(function ($controller, $rootScope) { 
    scope = $rootScope.$new(); 
    AboutCtrl = $controller('AboutCtrl', { 
     '$scope': scope 
    }); 
    })); 

    it('should attach a list of awesomeThings to the scope', function() { 
    expect(scope.awesomeThings.length).toBe(3); 
    }); 
}); 

Quando ho eseguito questo semplice test, ottengo:

TypeError: 'undefined' is not a function (evaluating '$rootScope.addDashboardBox(gettextCatalog.getString('About'), 'bg-maroon', 
     'ion-information', 0, 'app.about.index')') 
    at client/app/modules/about/controllers/about.config.js:6 
    at invoke (client/app/bower_components/angular/angular.js:4203) 
    at client/app/bower_components/angular/angular.js:4025 
    at forEach (client/app/bower_components/angular/angular.js:323) 
    at createInjector (client/app/bower_components/angular/angular.js:4025) 
    at workFn (client/app/bower_components/angular-mocks/angular-mocks.js:2425) 
TypeError: 'undefined' is not an object (evaluating 'scope.awesomeThings') 
    at client/test/modules/about/controllers/about.ctrl.js:21 

Se ho impostato logLevel: LOG_DEBUG, il file * circa mostrare:

->% grep su /tmp/karma-debug.log

client/app/modules/about/app.about.js 
    client/app/modules/about/controllers/about.config.js 
    client/app/modules/about/controllers/about.controller.js 
    client/app/modules/about/controllers/about.routes.js 
    client/test/modules/about/controllers/about.ctrl.js 
DEBUG [web-server]: serving (cached): client/app/modules/about/app.about.js 
DEBUG [web-server]: serving (cached): client/app/modules/about/controllers/about.config.js 
DEBUG [web-server]: serving (cached): client/app/modules/about/controllers/about.controller.js 
DEBUG [web-server]: serving (cached): client/app/modules/about/controllers/about.routes.js 
DEBUG [web-server]: serving (cached): client/test/modules/about/controllers/about.ctrl.js 

So che mi manca qualcosa di fondamentale, ma posso sembra di trovare cosa.

+0

hai controllato se le cose che dice non sono definite o meno? – Transcendence

+0

Definitivamente definito. Se lo aggiungo alla vista, appare. –

risposta

7

Non ho guardato con attenzione l'errore iniziale. L'errore effettivo era in $rootScope.addDashboardBox, che indicava che i moduli aggiuntivi dovevano essere inclusi.

Solution è per lo script di test per essere:

'use strict'; 

    describe('Controller: AboutCtrl', function() { 
    var AboutCtrl, 
     scope; 

    // load the controller's module 
    beforeEach(module('ui.router')); 
    beforeEach(module('gettext')); 
    beforeEach(module('formly')); 
    beforeEach(module('angular-loading-bar')); 
    beforeEach(module('lbServices')); 
    beforeEach(module('com.module.core')); 
    beforeEach(module('com.module.settings')); 
    beforeEach(module('com.module.about')); 

    // Initialize the controller and a mock scope 
    beforeEach(inject(function ($controller, $rootScope) { 
     scope = $rootScope.$new(); 
     AboutCtrl = $controller('AboutCtrl', { 
     '$scope': scope 
     }); 
    })); 

    it('should attach a list of awesomeThings to the scope', function() { 
     expect(scope.awesomeThings.length).toBe(3); 
    }); 

    }); 
7

Per me il futuro dal momento che è il primo risultato di Google.

Cerca dipendenze esterne!

Il log di Karma è un po 'fuorviante, il vero problema è che il modulo principale non è in esecuzione. Ad esempio, angular-stripe, che viene iniettato da Bower, karma.conf.js, richiede l'effettiva libreria Stripe JS caricata altrimenti si blocca l'intera applicazione (che è molto noiosa). Ho aggiunto questa linea a karma.conf.js:

files: [ 
    'https://js.stripe.com/v2', 

e ora funziona.

+0

Avevo altri script che non erano nella cartella scripts o bower_components, ho cambiato "app/scripts/**/*. Js" in "app/**/*. Js", significa qualsiasi script all'interno della cartella app. Grazie per l'indizio ... :-) –