karma: TypeError: Cannot read property 'prototype' of undefined
karma: TypeError: Cannot read property 'prototype' of undefined
每当我尝试注入 service
时,我都会收到以下错误。
(function () {
describe('Test', function () {
var $scope, $state, service,ctrl;
beforeEach(function() {
module('moduleApp');
module(function($provide) {
$provide.service('AppService', service);
});
inject(function($rootScope,$injector,$controller,AppService) {
$scope = $rootScope.$new();
$state = $injector.get('$state');
service = $injector.get('AppService');
ctrl = $controller('MyCtrl', {
$scope: $scope,
$state: $state,
AppService: service
});
});
});
it('should have a working UserSession service', function() {
expect(service).to.exist;
});
});
}());
错误:
TypeError: Cannot read property 'prototype' of undefined
at Object.instantiate (bower_components/angular/angular.js:4577:82)
at Object.<anonymous> (bower_components/angular/angular.js:4438:24)
at Object.invoke (bower_components/angular/angular.js:4570:17)
at Object.enforcedReturnValue [as $get] (bower_components/angular/angular.js:4422:37)
at Object.invoke (bower_components/angular/angular.js:4570:17)
at bower_components/angular/angular.js:4387:37
at getService (bower_components/angular/angular.js:4529:39)
at Object.invoke (bower_components/angular/angular.js:4561:13)
at Context.workFn (bower_components/angular-mocks/angular-mocks.js:2524:20)
at window.inject.angular.mock.inject (bower_components/angular-mocks/angular-mocks.js:2496:37)
Error: Declaration Location
at window.inject.angular.mock.inject (bower_components/angular-mocks/angular-mocks.js:2489:25)
at Context.<anonymous> (test/unit/app/views/Test.js:17:13)
你想干什么?!首先你告诉 karma 使用一个未定义的对象 service
作为 AppService
然后你尝试从注入器中获取 AppService
并将其分配给 service
?这是一个循环。
如果被测单元是AppService
,就不需要这样了:
module(function($provide) {
$provide.service('AppService', service);
});
如果您需要这项服务,您只需这样做:
inject(function($rootScope,$injector,$controller, _AppService_) {
$scope = $rootScope.$new();
$state = $injector.get('$state');
service = _AppService_;
ctrl = $controller('MyCtrl', {
$scope: $scope,
$state: $state,
AppService: service
});
});
如果被测单元依赖AppService
,你先构建一个模拟服务,然后使用provide
,记住你不需要再次注入它。
每当我尝试注入 service
时,我都会收到以下错误。
(function () {
describe('Test', function () {
var $scope, $state, service,ctrl;
beforeEach(function() {
module('moduleApp');
module(function($provide) {
$provide.service('AppService', service);
});
inject(function($rootScope,$injector,$controller,AppService) {
$scope = $rootScope.$new();
$state = $injector.get('$state');
service = $injector.get('AppService');
ctrl = $controller('MyCtrl', {
$scope: $scope,
$state: $state,
AppService: service
});
});
});
it('should have a working UserSession service', function() {
expect(service).to.exist;
});
});
}());
错误:
TypeError: Cannot read property 'prototype' of undefined
at Object.instantiate (bower_components/angular/angular.js:4577:82)
at Object.<anonymous> (bower_components/angular/angular.js:4438:24)
at Object.invoke (bower_components/angular/angular.js:4570:17)
at Object.enforcedReturnValue [as $get] (bower_components/angular/angular.js:4422:37)
at Object.invoke (bower_components/angular/angular.js:4570:17)
at bower_components/angular/angular.js:4387:37
at getService (bower_components/angular/angular.js:4529:39)
at Object.invoke (bower_components/angular/angular.js:4561:13)
at Context.workFn (bower_components/angular-mocks/angular-mocks.js:2524:20)
at window.inject.angular.mock.inject (bower_components/angular-mocks/angular-mocks.js:2496:37)
Error: Declaration Location
at window.inject.angular.mock.inject (bower_components/angular-mocks/angular-mocks.js:2489:25)
at Context.<anonymous> (test/unit/app/views/Test.js:17:13)
你想干什么?!首先你告诉 karma 使用一个未定义的对象 service
作为 AppService
然后你尝试从注入器中获取 AppService
并将其分配给 service
?这是一个循环。
如果被测单元是AppService
,就不需要这样了:
module(function($provide) {
$provide.service('AppService', service);
});
如果您需要这项服务,您只需这样做:
inject(function($rootScope,$injector,$controller, _AppService_) {
$scope = $rootScope.$new();
$state = $injector.get('$state');
service = _AppService_;
ctrl = $controller('MyCtrl', {
$scope: $scope,
$state: $state,
AppService: service
});
});
如果被测单元依赖AppService
,你先构建一个模拟服务,然后使用provide
,记住你不需要再次注入它。