Angular 2 / Jasmine / Karma error: Error: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?). in config/karma-shim.js
Angular 2 / Jasmine / Karma error: Error: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?). in config/karma-shim.js
这是服务:
import { Injectable } from '@angular/core';
import { Router, Event, NavigationStart, NavigationEnd, NavigationCancel, NavigationError } from '@angular/router';
@Injectable()
export class LoaderService {
public shouldShowLoader: boolean = false;
constructor(private router: Router) {
router.events.subscribe((event: Event) => {
this.navigationInterceptor(event);
});
}
// Shows and hides the loading spinner during Event changes
navigationInterceptor(event: Event): void {
if (event instanceof NavigationStart) {
this.shouldShowLoader = true;
}
else if (event instanceof NavigationEnd) {
this.shouldShowLoader = false;
}
// Set loading state to false in both of the below events to hide the spinner in case a request fails
else if (event instanceof NavigationCancel) {
this.shouldShowLoader = false;
}
else if (event instanceof NavigationError) {
this.shouldShowLoader = false;
}
else {
this.shouldShowLoader = false;
}
}
}
这是失败的测试:
import { TestBed, inject } from '@angular/core/testing';
import { Router } from '@angular/router';
import { LoaderService } from './loader.service';
describe('LoaderServiceTest', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ LoaderService, Router ]
});
});
it('#LoaderService should be defined', inject([LoaderService, Router], (service: LoaderService) => {
expect(service).toBeDefined();
}));
});
不确定为什么会失败?谷歌搜索类似问题,我只能找到 Angular 2 beta 的答案...我们使用的是最新的 Angular 2 / 2.2.0
您的测试失败,因为您没有向测试模块提供路由器需要实例化的所有参数。话虽如此,在单元测试中,不建议使用像路由器这样的服务的实际实现。更好的选择是制作这样的存根(我在这里为路由器内部的函数添加了一个间谍,以演示如果您想检查该函数是否已在某个时刻被调用将如何完成):
class RouterStub {
navigateByUrl = jasmine.createSpy('navigateByUrl');
}
然后在配置测试模块时:
providers: [
...,
{ provide: Router, useClass: RouterStub }
]
如果您想了解有关如何使用模拟和单元测试设置的更多信息,可以在此处的官方文档中找到:https://angular.io/docs/ts/latest/guide/testing.html
这是服务:
import { Injectable } from '@angular/core';
import { Router, Event, NavigationStart, NavigationEnd, NavigationCancel, NavigationError } from '@angular/router';
@Injectable()
export class LoaderService {
public shouldShowLoader: boolean = false;
constructor(private router: Router) {
router.events.subscribe((event: Event) => {
this.navigationInterceptor(event);
});
}
// Shows and hides the loading spinner during Event changes
navigationInterceptor(event: Event): void {
if (event instanceof NavigationStart) {
this.shouldShowLoader = true;
}
else if (event instanceof NavigationEnd) {
this.shouldShowLoader = false;
}
// Set loading state to false in both of the below events to hide the spinner in case a request fails
else if (event instanceof NavigationCancel) {
this.shouldShowLoader = false;
}
else if (event instanceof NavigationError) {
this.shouldShowLoader = false;
}
else {
this.shouldShowLoader = false;
}
}
}
这是失败的测试:
import { TestBed, inject } from '@angular/core/testing';
import { Router } from '@angular/router';
import { LoaderService } from './loader.service';
describe('LoaderServiceTest', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ LoaderService, Router ]
});
});
it('#LoaderService should be defined', inject([LoaderService, Router], (service: LoaderService) => {
expect(service).toBeDefined();
}));
});
不确定为什么会失败?谷歌搜索类似问题,我只能找到 Angular 2 beta 的答案...我们使用的是最新的 Angular 2 / 2.2.0
您的测试失败,因为您没有向测试模块提供路由器需要实例化的所有参数。话虽如此,在单元测试中,不建议使用像路由器这样的服务的实际实现。更好的选择是制作这样的存根(我在这里为路由器内部的函数添加了一个间谍,以演示如果您想检查该函数是否已在某个时刻被调用将如何完成):
class RouterStub {
navigateByUrl = jasmine.createSpy('navigateByUrl');
}
然后在配置测试模块时:
providers: [
...,
{ provide: Router, useClass: RouterStub }
]
如果您想了解有关如何使用模拟和单元测试设置的更多信息,可以在此处的官方文档中找到:https://angular.io/docs/ts/latest/guide/testing.html