karma: TypeError: Cannot read property 'prototype' o
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
,并且记住您不需要再次注入它。
相关文章:
- $(this).prop('property') vs. this.property
- 直接在函数声明上使用function.prototype.bind
- 如何迭代Array.prototype函数
- 为什么JavaScript可以'找不到给定的InnerHTML并返回Cannot set property
- Object.prototype using 'this'
- 在不兼容的接收器上调用的方法Set.prototype.add未定义
- 为什么可以't我用Set对象调用Array.prototype.map
- ExtJS 4 Object.prototype fail
- 如何通过Prototype或jquery移除子类的父类基类
- 复选框:使用Array.prototype.forEach调用推送选中订单,
- 将setTimeout()包装器实现为Element.prototype中的方法
- Object.prototype.hasOwnProperty.call() vs Object.prototype.h
- 出现了" Cannot read property 'prototype'undefined"
- ConstructorFunc.property vs ConstructorFunc.prototype.proper
- highcharts canvas-tools:无法读取property 'prototype'的定义
- 无法读取property 'prototype'Nodejs中未定义的
- B扩展了A,但B.add填充了A.prototype.property
- JavaScript prototype property and prototype link
- karma: TypeError: Cannot read property 'prototype' o
- Express -未捕获的TypeError: Cannot read property 'prototype&