2016-10-17 5 views
0

Meine angularjs-Tests sind alle fehlerhaft, weil der Controller nicht definiert ist. Ich habe verschiedene Lösungen ausprobiert, aber nichts funktioniert. Ich habe jede js-Datei, die ich dachte, in Karma benötigt, von den Looks davon Ich glaube nicht, dass die beforeEach (inject ... -Funktion aufgerufen wird oder wenn es dann etwas ist nicht richtig gesetzt wird. Ich fügte print Aussagen auf dem Weg, aber nichts in der before gedruckt wird (injizieren ... FunktionsaufrufKarma/Jasmine-Tests scheitern an: TypeError: MysmartdevicesCtrl ist nicht definiert

Fehlerprotokoll.

Firefox 49.0.0 (Ubuntu 0.0.0) Controller: RegisteruserCtrl should attach a list of awesomeThings to the scope FAILED 
    minErr/<@bower_components/angular/angular.js:68:12 
    loadModules/<@bower_components/angular/angular.js:4640:15 
    [email protected]_components/angular/angular.js:321:11 
    [email protected]_components/angular/angular.js:4601:5 
    [email protected]_components/angular/angular.js:4523:19 
    [email protected]_components/angular-mocks/angular-mocks.js:3074:44 
    [3]</ContextKarma/[email protected]://localhost:9876/context.js:151:7 
    TypeError: RegisteruserCtrl is undefined in test/spec/controllers/registeruser.js (line 21) 
    @test/spec/controllers/registeruser.js:21:5 
    [3]</ContextKarma/[email protected]://localhost:9876/context.js:151:7 
Firefox 49.0.0 (Ubuntu 0.0.0): Executed 3 of 3 (3 FAILED) ERROR (0.17 secs/0.09 secs) 

registerCtrl.js Spec:

'use strict'; 

describe('Controller: RegisteruserCtrl', function() { 

    // load the controller's module 
    beforeEach(module('clientApp')); 

    var RegisteruserCtrl, 
    scope; 

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

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

karma.conf.js

// Karma configuration 
// Generated on Tue Oct 11 2016 18:25:32 GMT-0500 (CDT) 
"use strict"; 
module.exports = function(config) { 
    config.set({ 

     // base path that will be used to resolve all patterns (eg. files, exclude) 
     basePath: '../', 


     // frameworks to use 
     // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
     frameworks: ['jasmine'], 


     // list of files/patterns to load in the browser 
     files: [ 

      'bower_components/angular/angular.js', 
      'bower_components/angular-route/angular-route.js', 
      'bower_components/angular-mocks/angular-mocks.js', 
      'app/**/app.js', 
      'app/Common/*.js', 
      'app/scripts/controllers/*.js', 
      'test/**/*.js' 
     ], 


     // list of files to exclude 
     exclude: [], 


     // preprocess matching files before serving them to the browser 
     // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
     preprocessors: {}, 

     plugins: [ 
      'karma-firefox-launcher', 
      'karma-chrome-launcher', 
      'karma-jasmine', 
     ], 

     // test results reporter to use 
     // possible values: 'dots', 'progress' 
     // available reporters: https://npmjs.org/browse/keyword/karma-reporter 
     reporters: ['progress'], 


     // web server port 
     port: 9876, 


     // enable/disable colors in the output (reporters and logs) 
     colors: true, 


     // level of logging 
     // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
     logLevel: config.LOG_INFO, 


     // enable/disable watching file and executing tests whenever any file changes 
     autoWatch: true, 


     // start these browsers 
     // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
     browsers: ['Firefox'], 


     // Continuous Integration mode 
     // if true, Karma captures browsers, runs the tests and exits 
     singleRun: true 
    }); 
}; 

Antwort

0

Mit dem minimatch Muster (/ **/*):

'app/**/app.js', 
'app/Common/*.js', 

verursacht mir eine Menge Ärger. Schreiben Sie stattdessen den Namen des Controllers (oder der Datei), den Sie benötigen. Zum Beispiel:

app/Common/RegisteruserCtrl.js 

Hier ist ein Beispiel, das funktioniert für mich:

files: [ 
     './node_modules/angular/angular.js',        // angular 
     './node_modules/angular-mocks/angular-mocks.js',     // angular-mocks 
     './app/views/js/controllers/sugestionController.js'  
    ], 
Verwandte Themen