Angular 测试在 运行 孤立时有效,但在与其他测试一起时失败
Angular test works when run in isolation, but fails when when with other tets
我 运行 遇到了一个问题,即当它与我的应用程序中的所有其他测试一起 运行 时,测试一直失败。返回的错误是
Uncaught TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. thrown
以下是题目中的两个类和测试文件
通知Class
export class Notification {
message: string;
category: string;
clearAll: boolean = false;
constructor(message: string, category?: string, clear?: boolean) {
this.message = message;
if (category) {
this.category = category;
}
if (clear) {
this.clearAll = clear;
}
}
}
通知服务Class
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Notification } from '../shared/notification';;
@Injectable({
providedIn: 'root'
})
export class NotificationsService {
notificationSubject: Subject<Notification>;
notification$: Observable<any>;
constructor() {
this.notificationSubject = new Subject<Notification>();
this.notification$ = this.notificationSubject.asObservable();
}
getNotificationObservable(): Observable<Notification> {
return this.notification$;
}
/**
* Method allowing a notification to be added so that subsribers can deal with is according.
* @param {Notification}notification
*/
addNotifications(notification: Notification): void {
this.notificationSubject.next(notification);
}
}
notificationService.spec.ts
import { NotificationsService } from './notifications.service';
describe('NotificationService', () => {
let service: NotificationsService;
beforeEach(() => { service = new NotificationsService(); });
it('to be created', () => {
expect(1 === 1).toBeTruthy();
});
});
如果我 运行 专注于此测试,它就会通过。即
fit('to be created', () => {
expect(1 === 1).toBeTruthy();
});
根据我所做的搜索,似乎有以下建议:
- 以前的测试没有正确地重置测试台,这就是为什么测试单独成功但在 运行 与其他
时失败的原因
- 或者通知 Class 在测试之间共享 属性,这导致了问题。
我怀疑第二个项目符号可能是这种情况,但我似乎无法确定问题所在。
我遇到了同样的问题。问题不在于失败的测试,而在于之前的测试。我最终删除了之前的测试,并解决了这个问题。
我 运行 遇到了一个问题,即当它与我的应用程序中的所有其他测试一起 运行 时,测试一直失败。返回的错误是
Uncaught TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. thrown
以下是题目中的两个类和测试文件
通知Class
export class Notification {
message: string;
category: string;
clearAll: boolean = false;
constructor(message: string, category?: string, clear?: boolean) {
this.message = message;
if (category) {
this.category = category;
}
if (clear) {
this.clearAll = clear;
}
}
}
通知服务Class
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Notification } from '../shared/notification';;
@Injectable({
providedIn: 'root'
})
export class NotificationsService {
notificationSubject: Subject<Notification>;
notification$: Observable<any>;
constructor() {
this.notificationSubject = new Subject<Notification>();
this.notification$ = this.notificationSubject.asObservable();
}
getNotificationObservable(): Observable<Notification> {
return this.notification$;
}
/**
* Method allowing a notification to be added so that subsribers can deal with is according.
* @param {Notification}notification
*/
addNotifications(notification: Notification): void {
this.notificationSubject.next(notification);
}
}
notificationService.spec.ts
import { NotificationsService } from './notifications.service';
describe('NotificationService', () => {
let service: NotificationsService;
beforeEach(() => { service = new NotificationsService(); });
it('to be created', () => {
expect(1 === 1).toBeTruthy();
});
});
如果我 运行 专注于此测试,它就会通过。即
fit('to be created', () => {
expect(1 === 1).toBeTruthy();
});
根据我所做的搜索,似乎有以下建议:
- 以前的测试没有正确地重置测试台,这就是为什么测试单独成功但在 运行 与其他 时失败的原因
- 或者通知 Class 在测试之间共享 属性,这导致了问题。
我怀疑第二个项目符号可能是这种情况,但我似乎无法确定问题所在。
我遇到了同样的问题。问题不在于失败的测试,而在于之前的测试。我最终删除了之前的测试,并解决了这个问题。