0

我开始用 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 secs / 0.011 secs)

我有简单的控制器:

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({});
});

});

4

2 回答 2

2

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

于 2013-07-29T19:58:25.587 回答
1

解决方案是将其包含在 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'
],
于 2014-07-30T07:57:21.520 回答