我知道这可能是非常愚蠢的问题。但是,如果有人可以帮助我,那将是惊人的。 我拥有的初始组件:
import { Injectable } from '@angular/core';
import { Router, NavigationStart } from '@angular/router';
import { Observable } from 'rxjs';
import { Subject } from 'rxjs';
@Injectable()
export class AlertService {
public subject = new Subject<any>();
public keepAfterNavigationChange = false;
constructor(public router: Router) {
// clear alert message on route change
router.events.subscribe(event => {
if (event instanceof NavigationStart) {
if (this.keepAfterNavigationChange) {
// only keep for a single location change
this.keepAfterNavigationChange = false;
} else {
// clear alert
this.subject.next();
}
}
});
}
success(message: string, keepAfterNavigationChange = false) {
this.keepAfterNavigationChange = keepAfterNavigationChange;
this.subject.next({ type: 'success', text: message });
}
error(message: any, keepAfterNavigationChange = false) {
this.keepAfterNavigationChange = keepAfterNavigationChange;
this.subject.next({ type: 'error', text: message.error.error });
}
getMessage(): Observable<any> {
return this.subject.asObservable();
}
}
我正在尝试测试这部分代码:
public subject = new Subject<any>();
public keepAfterNavigationChange = false;
constructor(public router: Router) {
// clear alert message on route change
router.events.subscribe(event => {
if (event instanceof NavigationStart) {
if (this.keepAfterNavigationChange) {
// only keep for a single location change
this.keepAfterNavigationChange = false;
} else {
// clear alert
this.subject.next();
}
}
});
}
但是运行ng test --coverage已通过此单元测试:
describe('AlertService', () => {
let service: AlertService;
let httpMock: HttpTestingController;
const message: any = {
message: 'success/error',
error: {
error: 'error'
}
};
const afterNavChange = false;
const wrongAfterChange = true;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [AlertService, HttpTestingController],
imports: [RouterTestingModule],
});
service = TestBed.get(AlertService); // get service instance
httpMock = TestBed.get(HttpTestingController);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
it('should return false on success call', () => {
service.success(message, afterNavChange);
spyOn(service, 'success');
expect(service.keepAfterNavigationChange).toBe(false);
// expect(service.subject.next).toHaveBeenCalled();
});
it('should return false on error call', () => {
service.error(message, afterNavChange);
spyOn(service, 'error');
expect(service.keepAfterNavigationChange).toBe(false);
// expect(service.subject.next).toHaveBeenCalled();
});
describe('Testing construction', () => {
let mockStart;
let mockResponse;
beforeEach(() => {
service.keepAfterNavigationChange = true;
});
it('should set isLoading to true', () => {
mockStart = of(new NavigationStart(0, '/'));
mockResponse = of(new NavigationStart(0, '/'));
spyOn(service.router.events, 'subscribe').and.returnValue(of(mockStart));
expect(service.keepAfterNavigationChange).toBe(true);
});
});
});
但始终显示此功能未涵盖:
event => {
if (event instanceof NavigationStart) {
if (this.keepAfterNavigationChange) {
// only keep for a single location change
this.keepAfterNavigationChange = false;
} else {
// clear alert
this.subject.next();
}
}
我应该如何精确测试?