【问题标题】:How to pass AngularJS $scope in karma test如何在业力测试中通过 AngularJS $scope
【发布时间】:2013-07-28 19:50:25
【问题描述】:

我开始用 Karma 编写一些测试,为了乞求,我决定尝试简单的测试。但是,我得到了两个错误(一个与茉莉花有关,另一个与注入有关(尝试 angular.inject 时出现相同的错误):

two errors Firefox 22.0 (Windows) ConfigurationController encountered a declaration exception FAILED
    TypeError: this.func.apply is not a function in 
/adapter/lib/jasmine.js?1374202126000 (line 1145)
    testacular.js:106
    :9876/context.html:40

    ReferenceError: inject is not defined in     /var/lib/tomcat7/webapps/lunchtime/test/controllers/configuration-controller.js (line 7)
    @/var/lib/tomcat7/webapps/lunchtime/test/controllers/configuration-controller.js:7
    @/var/lib/tomcat7/webapps/lunchtime/test/controllers/configuration-controller.js:3

Firefox 22.0 (Windows):执行 1 of 1 (1 FAILED)(0.48 秒 / 0.011 秒)

我有简单的控制器:

app.controller("ConfigurationController", ["$scope", "$http", function($scope, $http) {
$scope.configuration = {};
}]);

还有简单的测试:

'use strict';

describe('ConfigurationController', function() {
var scope, ctrl;
//you need to indicate your module in a test
beforeEach(angular.module('AmphinicyLunch'));
beforeEach(inject(function($rootScope) {
    scope = $rootScope.$new();
    ctrl = $controller("ConfigurationController", {$scope: scope})
}));

it("should have defined configuration", function($scope) {
    dump($scope.configuration);
    expect($scope.configuration).toEqual({});
});

});

【问题讨论】:

    标签: testing angularjs scope inject karma-runner


    【解决方案1】:

    对于注入错误,您需要包含 angular-mocks.js。模块和注入都在该文件中定义。恐怕我不知道 Jasmine 错误。

    【讨论】:

      【解决方案2】:

      解决方案是将其包含在 karma.conf.js 中:

      files: [
        'app/bower_components/angular/angular.js',
        'app/bower_components/angular-mocks/angular-mocks.js', <--------- notice mocks here
        'app/bower_components/angular-resource/angular-resource.js',
        'app/bower_components/angular-cookies/angular-cookies.js',
        'app/bower_components/angular-sanitize/angular-sanitize.js',
        'app/bower_components/angular-route/angular-route.js',
        'app/scripts/*.js',
        'app/scripts/**/*.js',
        'test/mock/**/*.js',
        'test/spec/**/*.js'
      ],
      

      【讨论】:

        猜你喜欢
        • 2016-06-19
        • 2013-10-26
        • 2017-01-24
        • 2018-02-24
        • 2017-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        相关资源
        最近更新 更多