2015-01-06 10 views
8

Ho avuto un problema quando ho provato a testare il mio controller. Quando eseguo il test, ho ricevuto un erroreErrore Karma Argomento 'Controller' non è una funzione, non è stato definito

Error: [ng:areq] Argument 'MainCtrl' is not a function, got undefined http://errors.angularjs.org/1.3.8/ng/areq?p0=MainCtrl&p1=not%20a%20function%2C%20got%20undefined 
     at assertArg (/Users/tetianachupryna/project/bower_components/angular/angular.js:1577) 
     at assertArgFn (/Users/tetianachupryna/project/bower_components/angular/angular.js:1588) 
     at /Users/tetianachupryna/project/bower_components/angular/angular.js:8418 
     at /Users/tetianachupryna/project/src/spec/controllers/main-controller.spec.js:11 
     at /Users/tetianachupryna/project/src/spec/controllers/main-controller.spec.js:17 
     at /Users/tetianachupryna/project/node_modules/karma-jasmine/lib/adapter.js:184 
     at http://localhost:9877/karma.js:185 
     at http://localhost:9877/context.html:51 

So che SO è pieno di domande simili. Ma io sono un totale nullo in Angular e JS in generale, quindi quelle risposte non mi hanno aiutato. Da domande simili su SO ho scoperto che il mio problema è nella definizione sbagliata del controller, ma non riesco ancora a capire cosa ho fatto di sbagliato. Ho accumulato e sto chiedendo il tuo aiuto.

Prima di tutto ecco la mia src/app/index.js file in cui è definito il mio modulo

var app = angular.module('myModule', [ 
    'ngAnimate', 
    'ngSanitize', 
    'ngResource', 
    'ui.router', 
    'pascalprecht.translate', 
    'thing1', 
    'thing2']); 

Ecco src/app/controller/main-controller.js

angular.module('myModule').controller('MainCtrl', [ 
    '$scope', 
    '$state', 
    function ($scope, $state) { 
     $scope.state = $state; 
     //*** 
     $scope.isBigStep = function isBigStep() { 
     return $state.$current.step == 3; 
     };  
    }]); 

E infine questo un file con il test src/spec/controller/main-controller.spec.js

describe('MainCtrl', function() { 
    var scope, $state, createController; 

    beforeEach(inject(function ($rootScope, $controller) { 
    scope = $rootScope.$new(); 

    createController = function() { 
     return $controller('MainCtrl', { 
     '$scope': scope 
     }); 
    }; 
    })); 

    it('should make 3 as current step', function() { 
    var controller = createController(); 
    expect(scope.isBigStep()).toBe(true); 
    }); 
}); 

In karma config ho tutti quei file

files: [ 
     'bower_components/angular/angular.js', 
     'bower_components/angular-mocks/angular-mocks.js', 
     'src/app/index.js', 
     'src/app/controllers/*.js', 
     'src/spec/controllers/*.js' 
    ], 

Per eseguire il mio test che uso plug-karma-runner in RubyMine.

Sarei grato per qualsiasi aiuto!

+2

Ti manca il carico del modulo. Metti 'beforeEach (module ('myModule'));' before' beforeEach (inject (function ($ rootScope, $ controller) {'? – PSL

+0

Grazie, questo trucco ha aiutato. Potresti fare una risposta autonoma che potrei accettare? –

+0

Sure ha aggiunto una risposta. – PSL

risposta

13

Quello che ti manca è aggiungere il modulo nel hook beforeEach per l'impostazione di prova. Altrimenti il ​​controller non verrà trovato. Quindi aggiungi beforeEach(module('myModule')).

describe('MainCtrl', function() { 
    var scope, $state, createController; 

    beforeEach(module('myModule')); //<--- Hook module 

    beforeEach(inject(function ($rootScope, $controller) { 
    scope = $rootScope.$new(); 

    createController = function() { 
     return $controller('MainCtrl', { 
     '$scope': scope 
     }); 
    }; 
    })); 

    it('should make 3 as current step', function() { 
    var controller = createController(); 
    expect(scope.isBigStep()).toBe(true); 
    }); 
}); 
Problemi correlati