我在 Angular 4 项目中使用了 signalR。在执行单元测试时收到此错误
TypeError:新 Signalr 窗口中的非法构造函数
下面是注入到组件中的服务代码。
当我在单元测试时尝试在组件中注入相同的服务时,我遇到了这个问题。
服务
export class SignalRService {
starting$: Observable<any>;
error$: Observable<string>;
refreshData$: Observable<any>;
wideAlert$: Observable<any>;
updatedTime$: Observable<any>;
// These are used to feed the public observables
private startingSubject = new Subject<any>();
private errorSubject = new Subject<any>();
private wideAlertSubject = new Subject<any>();
private refreshDataSubject = new Subject<any>();
private updatedTimeSubject = new Subject<any>();
// These are used to track the internal SignalR state
public hubConnection: any;
public hubProxy: any;
signalrURL = ServiceConfig.URL.signalRURL ;
signalRHubName = ServiceConfig.HUB_NAME.dataHub ;
constructor(@Inject(SignalrWindow) private window: SignalrWindow,
@Inject("channel.config") private channelConfig: ChannelConfig,
private http: Http) {
this.error$ = this.errorSubject.asObservable();
this.starting$ = this.startingSubject.asObservable();
this.refreshData$ = this.refreshDataSubject.asObservable();
this.wideAlert$ = this.wideAlertSubject.asObservable();
this.updatedTime$ = this.updatedTimeSubject.asObservable() ;
this.hubConnection = this.window.$.hubConnection();
this.hubConnection.url = this.signalrURL ;
this.hubProxy = this.hubConnection.createHubProxy(this.signalRHubName);
//Define handlers for any errors
this.hubConnection.error((error: any) => {
// Push the error on our subject
this.errorSubject.next(error);
});
this.hubProxy.on('BroadcastRefresh',(data) => {
this.refreshDataSubject.next(data);
});
this.hubProxy.on('BroadcastWideAlert', (data) => {
this.wideAlertSubject.next(data);
});
this.hubProxy.on('BroadcastUpdatedTime', (data) => {
this.updatedTimeSubject.next(data);
});
}
start(): void {
this.hubConnection.start()
.done(() => {
this.startingSubject.next();
})
.fail((error: any) => {
this.startingSubject.error(error);
});
}
}