Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为单元测试模拟RxJs可观察Websocket_Javascript_Angular_Unit Testing_Rxjs - Fatal编程技术网

Javascript 如何为单元测试模拟RxJs可观察Websocket

Javascript 如何为单元测试模拟RxJs可观察Websocket,javascript,angular,unit-testing,rxjs,Javascript,Angular,Unit Testing,Rxjs,我正在使用Angular 4和RxJs创建一个web应用程序。在我的代码的一部分中,我使用:Observable.websocket(url)创建了一个websocket函数 这是我的密码: /** * Tries to open a websocket connection to a given URL. * @param url * @param subscription Previous subscription to this socket (it may be n

我正在使用Angular 4和RxJs创建一个web应用程序。在我的代码的一部分中,我使用:
Observable.websocket(url)创建了一个websocket函数

这是我的密码:

  /**
   * Tries to open a websocket connection to a given URL.
   * @param url
   * @param subscription Previous subscription to this socket (it may be necessary to close it before proceeding)
   * @returns an object containing the subscription and the websocket subject.
   */
  private openConnection(url: string, subscription: Subscription): { socket$: WebSocketSubject<any>, subscription: Subscription } {
    const socket$ = Observable.webSocket<string>(url);

    // Make sure we unsubscribe from the previous socket if necessary.
    if (subscription) {
      subscription.unsubscribe();
    }

    // Subscribe the recently created socket. Note: this must be done otherwise the socket won't work.
    const newSubscription = socket$.subscribe(
      (msg: string) => this.handleIncomingMessages(msg),
      (error: Error) => this.handleErrors(error),
      () => this.handleComplete());

    return { socket$: socket$, subscription: newSubscription };
  }
/**
*尝试打开与给定URL的websocket连接。
*@param-url
*@param subscription此套接字的上一个订阅(可能需要在继续之前关闭它)
*@返回包含订阅和websocket主题的对象。
*/
私有openConnection(url:string,订阅:subscription):{socket$:WebSocketSubject,订阅:subscription}{
const socket$=Observable.webSocket(url);
//如有必要,请确保我们取消订阅上一个套接字。
如果(订阅){
订阅。取消订阅();
}
//订阅最近创建的套接字。注意:必须这样做,否则套接字将无法工作。
const newSubscription=socket$.subscribe(
(msg:string)=>this.handleIncomingMessages(msg),
(错误:错误)=>此.handleErrors(错误),
()=>this.handleComplete());
返回{socket$:socket$,订阅:newSubscription};
}
我想做的下一件事是为我的websocket创建一个单元测试。然而,为了做到这一点,我需要创建一个模拟websocket,这样我就可以自由地发送和接收来自它的消息


有人知道怎么做吗?有没有可能使用Angular
MockBackend
来实现这一点?

正如OP提到的,答案是使用间谍

fakeSocket = new Subject<any>(); spyOn(Observable, 'webSocket').and.returnValue(fakeSocket);
fakeSocket=新主题();spyOn(可观察,'webSocket')。和.returnValue(fakeSocket);

你可以使用
spy
告诉我什么时候我调用
openConnection(stuff)
我想让你把这个还给我。这就是你想要的吗?嗨,是的,你是对的,间谍在这里工作得很好。我已将此添加到我的代码中,所有操作都有效:
const fakeSocket=new Subject();spyOn(可观察,'webSocket')。和.returnValue(fakeSocket)。谢谢你的帮助!请选择最好的答案,很高兴我能帮忙:)开个玩笑怎么样?