2016-08-26 4 views
0

私は最初の簡単なテストを実行しているときに、このエラーをどのように取得しているのですか?

TypeError: $controller is not a function 

このようなコントローラコード:テストは次のようになり

(function() { 
    'use strict'; 
    angular 
     .module('dpServerV2WebappRev2App.controllers') 
     .controller('MainCtrl', MainCtrl); 
     MainCtrl.$inject = ['$scope']; 
     function MainCtrl($scope) { 
      $scope.x = 'x'; 
     } 
    })(); 

describe('MainCtrl', function() { 

    beforeEach(module('dpServerV2WebappRev2App.controllers')); 

    var $controller; 

    beforeEach(inject(function(_$controller_){ 
    $controller = _$controller_; 
    })); 

    describe('$scope.brand', function() { 
     it('should match the brand portal name', function() { 
      var $scope = {}; 
      var controller = $controller('MainCtrl', { $scope: $scope }); 
      expect($scope.x).toEqual('x'); 
     }); 
    }); 

}); 

ただ、より多くの事をクリアする私は、私がテストし、テストコードをコメントアウトしています$scope.xと交換してください:

expect(1).toEqual(1); 

そのため、私は私がテストで私のモジュールの注入をコメントアウトし、現在テストがモジュールを含むときが通過しているこのエラー

at Error (native) 
     at node_modules/angular/angular.min.js:6:412 
     at node_modules/angular/angular.min.js:40:134 
     at r (node_modules/angular/angular.min.js:7:355) 
     at g (node_modules/angular/angular.min.js:39:222) 
     at Object.db [as injector] (node_modules/angular/angular.min.js:43:246) 
     at Object.workFn (node_modules/angular-mocks/angular-mocks.js:3067:5 

を得ましたが、まだ上記のエラーを取得している: なし起こす新しいテストコード問題:

describe('MainCtrl', function() { 
    describe('$scope.brand', function() { 
     it('should match the brand portal name', function() { 
      expect(1).toEqual(1); 
     }); 
    }); 

それは私には設定の問題だので、私は私のカルマの設定ファイルを追加します:

var webpackConfig = require('./webpack.config.js'); 
webpackConfig.entry = {}; 

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: [ 
     'node_modules/angular/angular.min.js', 
     'node_modules/angular-mocks/angular-mocks.js', 
     'assets/app.bundle.js', 
     'app/*.js', 
     'tests/**/*.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: { 
     './assets/app.bundle.js': ['webpack'],  
    }, 

    webpack: webpackConfig, 

    webpackMiddleware: { 
     noInfo: true 
    }, 

    // 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: ['Chrome'], 


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

    // Concurrency level 
    // how many browser should be started simultaneous 
    concurrency: Infinity 
    }) 
} 
+0

あなたの角度のバージョンは何ですか:この

describe('MainCtrl', function() { beforeEach(module('dpServerV2WebappRev2App.controllers')); var controllerInjector, myController; beforeEach(inject(function($controller){ controllerInjector = $controller; })); describe('$scope.brand', function() { it('should match the brand portal name', function() { var $scope = {}; myController = controllerInjector('MainCtrl', { $scope: $scope }); expect($scope.x).toEqual('x'); }); }); }); 

デモを試してみてください?あなたは 'angle-mocks'をインストールしましたか? –

+0

こんにちはAngleバージョンは1.5.7です。角モックがインストールされ、Karma.conf.jsファイルに含まれています。角度は – user3462064

+0

です@GonzaloPincheiraArancibia私の質問編集を確認してください。 – user3462064

答えて

関連する問題