2016-10-03 101 views
12

我正在使用Angular 1.5.8。这里是我的代码:

describe('My Controller', function() { 
    var MyController; 
    var $controller; 
    var $rootScope; 
    var $state; 

    beforeEach(angular.mock.module('ui.router')); 
    beforeEach(module('app.my.ctrl')); 
    beforeEach(inject(function(_$controller_, _$rootScope_, _$state_) { 
     $controller = _$controller_; 
     $rootScope = _$rootScope_; 
     $state = _$state_; 
     MyController = $controller('MyController', { scope: $rootScope.$new() }); 
    })); 

    describe('#init', function() { 
     it('should do something', function() { 
      console.log('logStatement', MyController); 

      MyController.init(); 

      expect(true).toBe(true); 
     }) 

    }) 
}); 

测试运行器能够找到所有文件,所以这不是一个忘记加载的情况。当我运行这个测试,不仅在logStatement永远不会出现,我得到这个错误:

Argument 'MyController' is not a function, got undefined 

这是我的控制器:

(function() { 
'use strict'; 

angular 
    .module('app.my.ctrl') 
    .controller('MyController', MyController); 

MyController.$inject = [ 
    '$scope' 
]; 
/* ngInject */ 
function MyController($scope) { 

    var vm = this; 

    vm.hello = 'world'; 

    vm.init = function() { 
     return true; 
    } 
} 

})(); 

,这是我的人缘的conf文件:

// Karma configuration 

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-mocks/angular-mocks.js', 
     'bower_components/angular-ui-router/release/angular-ui-router.js', 
     'src/controllers/MyController.js', 
     'tests/unit/**/*.spec.js', 
    ], 


    // list of files to exclude 
    exclude: [ 
     '**/*.swp' 
    ], 


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


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

    // Spec Reporter Config 
    specReporter: { 
    //  suppressErrorSummary: false, 
    //  suppressFailed: false, 
    //  suppressPassed: false, 
     suppressSkipped: true 
    //  showSpecTiming: false 
    }, 


    // 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: true, 

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

这是什么意思?我无法在文档中找到任何解释这一点的内容。

UPDATE:

我读过this answer和答案没有奏效。

+0

您的控制器定义在哪个模块的名称是什么? – segFault

+0

请阅读片段。 ''app.my.ctrl'' – dopatraman

+0

我读到了,只是确定是正确的。 – segFault

回答

3

试图改变从scope控制器注入的服务$scope

beforeEach(inject(function(_$controller_, _$rootScope_, _$state_) { 
     $controller = _$controller_; 
     $rootScope = _$rootScope_; 
     $state = _$state_; 
     MyController = $controller('MyController', { $scope: $rootScope.$new()}); 
})); 
+0

这当然是*下一个问题* OP会遇到但不是当前的:) – Phil

+0

请只添加已经过测试的答案。 – dopatraman

+0

除了这个答案,使用你的代码适用于我。我向你推荐,检查你的控制器.js文件是否已在业力流程中加载。为此,使用'karma start --no-single-run'命令在url ** http:// localhost:9876/debug.html中打开您的浏览器。尝试通过示例在您的'use strict'行之后打印控制器console.log或检查控制器是否已加载在Chrome开发工具的'Sources'选项卡中 –

1

你尝试让你的PB尽可能简单?在应用程序内可以成功进行此调用吗? $controller('MyController', { $scope: $rootScope.$new()});。如果这有效(它实际上应该),问题肯定来自你的测试/茉莉/卡玛/咕噜/咕噜配置,你不应该挖角度方向了。你能否给我们看看你在应用程序中如何定义app.my.ctrl模块?也许这个模块依赖于更多模块,而不仅仅是你在测试中嘲笑的ui.router。如果是这种情况,则无法加载该模块,也无法创建任何内部控制器。

+0

yes 。没有骰子... – dopatraman

+0

“没有骰子”......我不明白。这是否意味着测试环境的简单调用不起作用?或者这是否意味着你的模块不包含更多的依赖关系。 (有关信息,模块定义是您在文章中未提及的唯一重要内容) – Stephane

0
describe('My Controller', function() { 
    var MyController; 
    var scope; 

    beforeEach(angular.mock.module('ui.router')); 
    beforeEach(module('app.my.ctrl')); 
    beforeEach(inject(function($rootScope) { 
     scope = $rootScope.$new(); 
    })); 

    describe('#init', function() { 
     it('should do something', function($componentController) { 
      var MyController = $componentController('MyController', { 
       $scope : scope 
      }); 
      MyController.init(); 

      expect(true).toBe(true); 
     }) 

    }) 
});