【发布时间】:2019-03-16 13:55:47
【问题描述】:
我遇到了一个问题,即当测试与我的应用程序中的所有其他测试一起运行时,它始终失败。返回的错误是
未捕获的类型错误:您在预期流的位置提供了“未定义”。您可以提供 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();
});
根据我所做的搜索,似乎有以下建议:
- 之前的测试没有正确地重置测试台,这就是为什么测试单独成功但与其他人一起运行时失败的原因
- 或者通知类在测试之间共享一个属性,这是导致问题的原因。
我怀疑第二个子弹可能是这种情况,但我似乎无法确定问题所在。
【问题讨论】:
-
为什么您认为问题出在通知类中?您的错误是关于缺少流并且通知不包含任何流。
标签: angular karma-jasmine angular-test