Angular中的单元测试socket.io. [英] Unit Testing socket.io in Angular

查看:119
本文介绍了Angular中的单元测试socket.io.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中对一个角度服务进行单元测试,这会创建socket.io客户端。我的服务看起来像这样:

  export class SocketService {
private name:string;
私有主机:string = window.location.protocol +//+ window.location.hostname +:3000;
socket:io.Socket;

constructor(){}

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

私人设置(名称:字符串){
this.name = name;
let socketUrl = this.host +/+ this.name;
this.socket = io.connect(socketUrl);
}
}

所以要写我的测试,我正在使用模拟-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');
});

(窗口为任意).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') ;
})
})));
});

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



< blockquote>

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


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



我如何解决这个问题?



更新:



我尝试的另一种方法是使用 async 例如

  it('应订阅dwnTime事件',async(inject([JobService],(service:JobService)=> {
service.subscribeEvent('line / 602')。subscribe(val => {
expect(val).toEqual('test 2');
})
})));;

然后我得到:


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


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

解决方案

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



jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; (该值是在describe函数内的任何地方都有 b
$ b



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


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

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:

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.

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

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.

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

解决方案

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)

anywhere inside the describe function.

Here you can find a full example.

这篇关于Angular中的单元测试socket.io.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆