我遇到了一个问题,即与我的应用程序中的所有其他测试一起运行时,测试始终会失败。返回的错误是
未捕获的TypeError:您在期望流的位置提供了“ undefined”。您可以提供一个Observable,Promise,Array或Iterable。抛出
以下是有问题的两个类和测试文件。
通知类别
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;
}
}
}
通知服务类别
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();
});
从我所做的搜索来看,似乎有以下两种建议:
我怀疑可能是第二个问题,但我似乎无法确定问题所在。
答案 0 :(得分:0)
我有同样的问题。问题不在失败的测试中,而是先前的测试。我最终删除了先前的测试,并解决了这个问题。