0

I have a class which use Socket as a private property.

import {Socket} from 'socket.io-client';

export class SocketService {
 
  private socket: Socket;
 
  public initializeSocket() {
      if(this.socket) { return; }
      this.socket = // get a new Socket
      this.socket.on('connect', () => this.processConnect() );
  }
  private processConnect() {
    ...
  }
}

How should I mock this Socket and validate on is called. Any suggestion would be appreciated!

Kerwen
  • 516
  • 5
  • 22

1 Answers1

0

Finally I solve it.

it('test socket on',()=>{
  service['socket'] = tempSocket;   // have to initialize it temply as spyOn need a object
  spyOn(service['socket'], 'on');
  service['socket'] = undefined;

  service.initializeSocket();

  expect(service['socket'].on).toHaveBeenCalled();
}
Kerwen
  • 516
  • 5
  • 22