【发布时间】:2018-01-26 03:53:10
【问题描述】:
我无法弄清楚这段代码有什么问题。它一直告诉我没有 AlertService 的提供者。我提供了 AlertServiceSpy 类,但它仍然说没有提供 AlertService。一旦我将 CoreModule 和 RouterTestingModule 添加到导入中,它就会开始工作。
这一定是因为 AlertService 对它们有依赖关系,但是在这个测试中这完全不重要。我只是想测试 AlertComponent 并且不关心 AlertService 的依赖关系,因此我模拟了它。为什么我需要将 AlertService 的依赖项导入此测试?请帮忙!
describe('AlertComponent', () => {
let comp: AlertComponent;
let fixture: ComponentFixture<AlertComponent>;
let de: DebugElement;
let el: HTMLElement;
let alertService: any;
class AlertServiceSpy {
getMessage = jasmine.createSpy('getMessage').and.callFake(
() => Observable
.create((observer) => {
return 'message';
})
);
}
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
],
declarations: [
AlertComponent
],
providers: [
{ provide: AlertService, useClass: AlertServiceSpy }
]
}).overrideComponent(AlertComponent, {
set: {
providers: [
{ provide: AlertService, useClass: AlertServiceSpy }
]
}
}).compileComponents();
fixture = TestBed.createComponent(AlertComponent);
}));
});
// TESTS
it('true is true', () => expect(true).toBe(true));
});
警报组件:
import { Component, OnInit } from '@angular/core';
import { AlertService } from '../Alert/alert.service';
@Component({
moduleId: module.id,
selector: 'app-alert-system',
templateUrl: 'alert.component.html',
})
export class AlertComponent implements OnInit {
public message: any;
constructor( private alertService: AlertService) {}
ngOnInit() {
this.alertService.getMessage().subscribe(
message => {
this.message = message;
});
}
}
警报服务:
import { Injectable } from '@angular/core';
import { Router, NavigationStart } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { HttpAlertGenerator } from './http-alert-generator'
/*
Service used for handling alerts.
*/
@Injectable()
export class AlertService {
private _subject = new Subject<any>();
private _keepAfterNavigationChange = false;
private _httpAlertGenerator = new HttpAlertGenerator();
constructor(private router: Router) {
router.events.subscribe(event => {
if (event instanceof NavigationStart) {
if (this._keepAfterNavigationChange) {
this._keepAfterNavigationChange = false;
} else {
this._subject.next();
}
}
});
}
// public
public httpAlert(errCode: number,
additionalInfo: string,
keepAfterNavigationChange = true,
displayDurationInMillis?: number) {
const message = additionalInfo + ' ' + this._httpAlertGenerator.codeToMessage(errCode);
if (errCode >= 300) {
this.error(message, keepAfterNavigationChange);
} else {
this.success(message, keepAfterNavigationChange);
}
}
public success(message: string, keepAfterNavigationChange = true, timeLimit = 5000) {
this._keepAfterNavigationChange = keepAfterNavigationChange;
this._subject.next({type: 'success', text: message});
this.prepareNext(timeLimit);
}
public error(message: string, keepAfterNavigationChange = true) {
this._keepAfterNavigationChange = keepAfterNavigationChange;
this._subject.next({type: 'error', text: message});
}
// FOR ALERT COMPONENT
public getMessage(): Observable<any> {
return this._subject.asObservable();
}
private prepareNext(timelimit = 5000) {
const nextSubj = this._subject;
setTimeout(function(){
nextSubj.next();
}, timelimit);
}
}
【问题讨论】:
-
您是否在 app.module.ts 中将 AlertService 声明为提供程序?也在 let alertService: any;可以让 alertService: AlertService;
-
我把它改成了alertService:AlertService。我在核心模块中提供它,我也将其导入 app.module
标签: angular typescript jasmine karma-runner