执行期间出错..
Error: [$injector:unpr] Unknown provider: secondServiceProvider <- secondService
my.component.controller.ts
import { FirstService } from '../../first.service';
import { SecondService } from '../../second.service';
export class MyComponentController {
constructor(
public $state: ng.ui.IStateService,
private firstService: FirstService,
private secondService: SecondService) { }
}
MyComponentController.$inject = [
'$state',
FirstService.registeredName,
SecondService.registeredName];
my.component.spec.ts
import { FirstService } from '../../first.service';
import { SecondService } from '../../second.service';
import { MyModule } from '../index';
describe('Component: my', () => {
let $componentController: angular.IComponentControllerService;
let firstService: FirstService;
let secondService: SecondService;
let scope: angular.IScope;
let $state: ng.ui.IStateService;
beforeEach(() => {
angular.mock.module('ui.router');
angular.mock.module(MyModule.name);
});
beforeEach(inject(
($rootScope: angular.IScope,
_$state_: ng.ui.IStateService,
_$componentController_: angular.IComponentControllerService) => {
scope = $rootScope.$new();
$state = _$state_;
firstService = new FirstService();
secondService = new SecondService();
$componentController = _$componentController_;
}));
describe('Controller: MyComponentController', () => {
it('should create an instance of MyComponentController', () => {
let ctrl: any = $componentController('my', { $scope: scope }, { $state: $state, firstService: firstService, secondService: secondService });
expect(ctrl).toBeDefined();
});
});
});
答案 0 :(得分:2)
$componentController
使用不当。第三个参数是绑定。模拟服务是依赖项,$scope
也是如此。它应该是:
$componentController('my', {
$scope: scope, $state: $state, firstService: firstService, secondService: secondService
});
以这种方式提供真正的依赖关系绝对没有意义,默认情况下会提供它们。根据经验,所有单位但经过测试的单位(控制器)都应该被嘲笑或存根。
真正的路由器不应该用于单元测试。应排除ui.router
,并在必要时对其服务进行模拟或删除。