控制器测试始终给出未定义的$ scope

时间:2015-08-14 20:39:37

标签: angularjs jasmine karma-runner

我只是想开始单元测试Angular代码,并尝试使用Karma和Jasmine进行测试。我已经检查了几个不同的教程并模拟了他们的代码,但每次都在我的测试中未定义$ scope。我一整天都在这上班,已经阅读了每一个相关的Stackoverflow问题(一对夫妇似乎相关但没有得到答案),让事情变得简单和简单,以缩小问题等等。我即将把我的头发拉出来。如果有人能帮助我,我会非常感激!

karma.config.js:

// Karma configuration
// Generated on Tue Aug 11 2015 11:40:35 GMT-0500 (Central Daylight Time)

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: [
      'Scripts/angular-min.js',
      'Scripts/angular-mocks.js',
      'Scripts/angular-route-min.js',
      'Scripts/angular-resource.min.js',
      'Scripts/hr-module.js',
      'Scripts/jquery-1.8.2.js',
      'Scripts/Employee/*.js'

    ],


    // list of files to exclude
    exclude: [
        'Scripts/_references.js',
        'Scripts/jquery-1.8.2.intellisense.js'
    ],


    // 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: ['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
  })
}

简单controller.js:

(function () {
    'use strict';

    angular.module('hrModule').controller('SimpleController', ['$scope', function ($scope) {        
        $scope.greeting = "hello world";        
    }]);


}());

测试简单控制器:

'use strict';

describe('SimpleController', function () {

    beforeEach(angular.mock.module('hrModule'));

    var $controller,
        scope,
        SimpleController;


    beforeEach(angular.mock.inject(function ($rootScope, _$controller_) {
        scope = $rootScope.$new();
        $controller = _$controller_;
        SimpleController = $controller('SimpleController', { $scope: scope });
    }));


    it('says hello world', function () {
        expect(scope).toBeDefined();
    });    

});

当我运行它时,我一直得到一个($ injector:modulerr)错误,并说"预期未定义为定义"。如果有人在这里看到任何问题,请告诉我!谢谢!

1 个答案:

答案 0 :(得分:1)

感谢PSL与我合作。我非常感谢你回来继续提供技巧。

然而,解决方案比那更平凡。我从Nuget(最新版本,1.4.x)获取的angular.js文件与我从教程链接(1.3.3)下载的angular-mock.js脚本之间存在不兼容性。由于某种原因,这导致了模块化错误,让我感到困惑,但我在重建一个较小的测试项目时使用相同的文件在浏览器中测试它。

因此,如果其他人收到这样的错误,请打开角度库文件并确保它们共享相同的版本号!