我遇到了一个问题,当测试在我的应用程序中运行所有其他测试时,测试失败。返回的错误是
未捕获的TypeError:您在预期的流中提供了“未定义”。您可以提供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();
});
从我所做的搜索中,似乎有一个建议:
我怀疑第二颗子弹可能就是这种情况,但我似乎无法确定问题。
我遇到过同样的问题。问题不在于测试失败,而是之前的测试。我最终删除了以前的测试,并解决了这个问题。