在angular中进行单元测试时出错:-“ TypeError:新SignalrWindow中的非法构造函数”

时间:2018-12-21 09:38:19

标签: angular signalr angular-unit-test

我在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);
            });
    }
}

0 个答案:

没有答案