1

I have subject and observable -

export class WebSocket{
    private _WebSocketSubject: Subject<any>;
    public OWebSockConn$: Observable<any>;

    constructor(){
          this._WebSocketSubject = new Subject<JSON>();
          this.OWebSockConn$ = this._WebSocketSubject.asObservable();
    }

    public receiveData = () => {
          this._WebSocketSubject$.next("hi");
    });
}

ReceiveData is called to push data. In Unit test i want to know the count of subscribers for this subject and i want to check whether data i receive is correct or not. So this is my unit test case -

Approach 1:

it('Find_Observanles_AndData_IsCorrect', async ((done) => {
   myWebSocket.receiveData();
   expect((myWebSocket as any).OWebSockConn$.observers.length).toBe(1);

   myWebSocket.invokeFromWebSockConn$.subscribe((data) => {
       console.log("Data reached");
       expect(data).not.tobe(null);
       done();
  });
}));

Approach 2:

it('Find_Observanles_AndData_IsCorrect', async(inject( [WebSocket], ( websocket ) => {
     myWebSocket.receiveData();
     expect((myWebSocket as any).OWebSockConn$.observers.length).toBe(1);
     websocket.OWebSockConn.subscribe(result =>  expect(data).not.tobe(null)); 
}));

It is not going inside subscribe and console is also not printing, and my subscriber count is 0 although it is subscribed in another service, i tried two approaches i don't know where is the issue, please help a noobie.

Tried approach mentioned here as well - Unit testing an observable in Angular 2

0 Answers0