![](/img/trans.png)
[英]Reactjs - × 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)
你想做什么?! 首先,您告訴業力使用未定義的對象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
,並記住您不需要再注入它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.