【问题标题】:Expect jasmine Spy to be called "eventually", before timeout期待 jasmine Spy 在超时之前被“最终”调用
【发布时间】:2021-02-12 00:37:03
【问题描述】:

我最近写了很多异步单元测试,结合使用 Angular 的 fakeAsync,从 async 测试体函数返回 Promises,Jasmine done 回调等。一般我已经能够让一切都以完全确定的方式工作。

我的代码的一些部分与非常复杂且难以模拟的第 3 方库以非常复杂的方式交互。我想不出一种方法来挂钩事件或生成保证在该库完成后台工作后解决的 Promise,所以目前我的测试使用setTimeout 卡住了:

class MyService {
  public async init() {
    // Assume library interaction is a lot more complicated to replace with a mock than this would be
    this.libraryObject.onError.addEventListener(err => {
      this.bannerService.open("Load failed!" + err);
    });
    // Makes some network calls, etc, that I have no control over
    this.libraryObject.loadData();
  }
}

it("shows a banner on network error", async done => {
    setupLibraryForFailure();
    await instance.init();

    setTimeout(() => {
        expect(banner.open).toHaveBeenCalled();
        done();
    }, 500);  // 500ms is generally enough... on my machine, probably
});

这让我很紧张,尤其是setTimeout 中的神奇数字。它的扩展性也很差,因为我确信 500 毫秒比我完成任何其他测试所需的时间要长得多。

我想我想做的是能够告诉 Jasmine 轮询 banner.open 间谍,直到它被调用,或者直到测试超时结束并且测试失败。然后,一旦错误处理程序被触发并完成,测试就应该注意到。有没有更好的方法,或者这是一个好主意?它是我没有看到的某个地方的内置模式吗?

【问题讨论】:

    标签: javascript unit-testing asynchronous jasmine


    【解决方案1】:

    我认为你可以利用callFake,基本上一旦调用了这个函数就会调用另一个函数。

    类似这样的:

    it("shows a banner on network error", async done => {
        setupLibraryForFailure();
        // maybe you have already spied on banner open so you have to assign the previous
        // spy to a variable and use that variable for the callFake
        spyOn(banner, 'open').and.callFake((arg: string) => {
          expect(banner.open).toHaveBeenCalled(); // maybe not needed because we are already doing callFake
          done(); // call done to let Jasmine know you're done
        });
        await instance.init();
    });
    

    我们在banner.open上设置了一个间谍,当它被调用时,它会使用callFake调用回调函数,我们在这个回调中调用done,让Jasmine知道我们的断言已经完成。

    【讨论】:

    • 这很简单。完美运行,谢谢!
    猜你喜欢
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    相关资源
    最近更新 更多