【发布时间】:2020-08-22 07:41:41
【问题描述】:
我在我的应用程序中使用了 EventSource 并编写了如下所示的包装服务:
import { Injectable } from '@angular/core';
import { EventSourcePolyfill } from 'event-source-polyfill';
import { Observable } from 'rxjs';
import { AppConstants } from 'src/app/framework/constants/app.constants';
const base_url = `${AppConstants.REST_API_URL}datacontrolmanagment/notification`;
@Injectable({
providedIn: 'root'
})
export class SseService {
private eventSource;
constructor() { }
/**
*
* @param url for Event Source Endpoint
* @param headers passing authorization token
*/
private getEventSource(headers) {
return new EventSourcePolyfill(base_url, headers)
}
public getServerSentEvents(headers) {
return Observable.create(observer => {
if (!(this.eventSource instanceof EventSourcePolyfill)) {
this.eventSource = this.getEventSource(headers);
}
this.eventSource.onmessage = msg => {
console.log(msg)
observer.next(msg);
};
this.eventSource.onerror = msg => {
};
});
};
public closeEventSource() {
if ((this.eventSource instanceof EventSourcePolyfill)) {
this.eventSource.close();
}
}
}
我已在我的一个组件中使用此服务。
为了代码覆盖率,我想测试此服务。 但是我无法弄清楚如何测试上面的代码。
我尝试创建 EventSource 的模拟/虚拟类,但没有成功
注意:我已经测试过这个服务的组件代码。
【问题讨论】:
标签: angular typescript jasmine karma-jasmine