Angular中的单元测试socket.io.

时间:2021-07-31 12:39:34

I am trying to unit test an angular service in my application, which creates socket.io client. My service looks something like this:

我试图在我的应用程序中单元测试角度服务,这将创建socket.io客户端。我的服务看起来像这样:

export class SocketService {
  private name: string;
  private host: string = window.location.protocol + "//" + window.location.hostname + ":3000";
  socket: io.Socket;

  constructor() { }

  subscribeEvent(name: string): Observable<IJobResp> {
    this.setup(name);
    return new Observable(observer => {
      this.socket.on('job', val => observer.next(val))
    })
  }

  private setup(name: string) {
    this.name = name;
    let socketUrl = this.host + "/" + this.name;
    this.socket = io.connect(socketUrl);
  }
}

So to write my test, I am using the mock-socket library to set up a mock socket.io server. Here is what the test looks like:

所以要编写我的测试,我使用mock-socket库来设置一个mock socket.io服务器。这是测试的样子:

describe('SocketService', () => {
  let socket;

  const mockServer = new Server('http://localhost:3000/');
  mockServer.on('connection', server => {
    mockServer.emit('job', 'test message 1');
  });

  (window as any).io = SocketIO;
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [JobService]
    });
  });

  it('should xyz, inject([JobService], fakeAsync((service: JobService) => {
    service.subscribeEvent('/').subscribe(val => {
      expect(val).toEqual('test message 1');
    })
  })));
});

However, that test throws the error:

但是,该测试会引发错误:

Error: Cannot make XHRs from within a fake async test.

错误:无法在伪异步测试中制作XHR。

If I don't have the fakeAsync, then the test passes before the assertion in the subscribe() is processed.

如果我没有fakeAsync,则在subscribe()中的断言被处理之前测试通过。

How do I get around this?

我该如何解决这个问题?

Update:

Another approach I have tried is to use async e.g.

我尝试过的另一种方法是使用异步,例如

  it('should subscribe to dwnTime events', async(inject([JobService], (service: JobService) => {
    service.subscribeEvent('line/602').subscribe(val => {
      expect(val).toEqual('test 2');
    })
  })));

Then I get:

然后我得到:

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

错误:超时 - 在jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时内未调用异步回调。

This looks like a timing issue, but I still am not sure why?

这看起来像是一个时间问题,但我仍然不确定为什么?

1 个解决方案

#1


1  

Your second approach is the right one as the mock server is a real one, therefore you need to increase the timeout by setting

您的第二种方法是正确的,因为模拟服务器是真实的,因此您需要通过设置来增加超时

jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; (the value is to be adjusted)

jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; (值要调整)

anywhere inside the describe function.

describe函数内的任何位置。

Here you can find a full example.

在这里你可以找到一个完整的例子。

#1


1  

Your second approach is the right one as the mock server is a real one, therefore you need to increase the timeout by setting

您的第二种方法是正确的,因为模拟服务器是真实的,因此您需要通过设置来增加超时

jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; (the value is to be adjusted)

jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; (值要调整)

anywhere inside the describe function.

describe函数内的任何位置。

Here you can find a full example.

在这里你可以找到一个完整的例子。