我开始用 Karma 编写一些测试,为了求助,我决定尝试简单的测试。但是,我收到两个错误(一个与 Jasmine 有关,另一个与注入(inject)有关(我在尝试 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 次,共 1 次(1 次失败)(0.48 秒/0.011 秒)
我有一个简单的 Controller :
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({});
});
});
最佳答案
对于注入(inject)错误,你需要包含angular-mocks.js。模块和注入(inject)都在该文件中定义。恐怕我不知道 Jasmine 错误。
关于testing - 如何在 karma 测试中通过 AngularJS $scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17912181/