简体   繁体   English

如何正确测试 Angular 中 setInterval() 中的语句?

[英]How to properly test statements in setInterval() in Angular?

I have this very simple code:我有这个非常简单的代码:

public async authenticate(username: string, password: string) {
    const authenticationResponse = await this.dataProvider.authenticate(username, password);

    if (authenticationResponse.result.code == 0) {
        //start interval for periodically checking authentication info
        this._authInfoIntervalId = setInterval(() => {
             this.getAuthInfo();
        }, 2000);

In my unit tests only line with this.getAuthInfo() is not concidered as covered.在我的单元测试中,仅包含 this.getAuthInfo() 的行未被视为涵盖。 Is it somehow possible to test this?是否有可能以某种方式对此进行测试? I have tried some approaches with jasmine.createSpy but nothing seemed to work (most likely because I was doing it wrong).我用 jasmine.createSpy 尝试了一些方法,但似乎没有任何效果(很可能是因为我做错了)。 Can someone please help me to get it right?有人可以帮我把它弄好吗? Thanks谢谢

UPDATE: I tried something like this更新:我试过这样的事情

it('should test interval somehow', () => {
  const intervalCallback = jasmine.createSpy("getAuthInfo");
  jasmine.clock().install();
  service.authenticate('username', 'password');
  jasmine.clock().tick(2001);
  expect(intervalCallback).toHaveBeenCalled();
})

and test fails instantly with AuthenticationService should test interval somehow FAILED Expected spy getAuthInfo to have been called.并且测试立即失败,AuthenticationService 应该以某种方式测试间隔 FAILED 预期间谍 getAuthInfo 已被调用。

SOLUTION: I had to spyOn also on dataProvider so I got right response to actually reach that part of code with interval解决方案:我还必须在 dataProvider 上进行 spyOn,所以我得到了正确的响应以实际到达具有间隔的那部分代码

  it('should test interval somehow', async () => {
    const intervalCallback = spyOn(service, 'getAuthInfo');
    spyOn(dataProvider, 'authenticate').and.returnValue(Promise.resolve(authenticateMockResponse));
    jasmine.clock().install();
    await service.authenticate('username', 'password');
    jasmine.clock().tick(2001);
    expect(intervalCallback).toHaveBeenCalled();
  });

jasmine.createSpy() should be used for creating a bare spy object, see the documentation . jasmine.createSpy()应该用于创建裸间谍 object,请参阅文档 It doesn't have any connection to your service, it's not what you are looking for.它与您的服务没有任何联系,这不是您要找的东西。

You want to spy a function on an existing object (in your case the service ), for which you can use the spyOn function.你想在现有的 object(在你的例子中是service )上监视 function,为此你可以使用spyOn function。

it('should test interval somehow', () => {
  const intervalCallback = spyOn(service, 'getAuthInfo');
  jasmine.clock().install();
  service.authenticate('username', 'password');
  jasmine.clock().tick(2001);
  expect(intervalCallback).toHaveBeenCalled();
})

In Angular, you can use the async and fakeAsync functions from the @angular/core/testing module to properly test statements within a setInterval() function.在 Angular 中,您可以使用 @angular/core/testing 模块中的 async 和 fakeAsync 函数来正确测试 setInterval() 中的语句 function。

First, you'll need to wrap your test in the fakeAsync function, which allows you to use the tick() function to advance the virtual time.首先,您需要将测试包装在 fakeAsync function 中,它允许您使用 tick() function 来提前虚拟时间。 Next, you'll need to wrap the code you want to test within the setInterval() function in an async function.接下来,您需要将要测试的代码包装在异步 function 中的 setInterval() function 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM