Angular 2测试-异步函数调用-何时使用 [英] Angular 2 Testing - Async function call - when to use

查看:105
本文介绍了Angular 2测试-异步函数调用-何时使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Angular 2中进行测试时,何时在 TestBed 中使用异步功能?

When do you use the async function in the TestBed when testing in Angular 2?

什么时候使用这个?

 beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [MyModule],
            schemas: [NO_ERRORS_SCHEMA],
        });
    });

您什么时候使用它?

beforeEach(async(() => {
    TestBed.configureTestingModule({
        declarations: [MyModule],
        schemas: [NO_ERRORS_SCHEMA],
    });
}));

有人可以启发我吗?

推荐答案

async将不允许下一个测试开始,直到async完成所有任务. async的作用是将回调包装在一个区域中,在该区域中跟踪所有异步任务(例如setTimeout).完成所有异步任务后,async完成.

async will not allow the next test to start until the async finishes all its tasks. What async does is wrap the callback in a Zone, where all asynchronous tasks (e.g. setTimeout) are tracked. Once all the asynchronous tasks are complete, then the async completes.

如果您曾经在Angular外使用Jasmine,则可能已经看到done被传递给了回调函数

If you have ever worked with Jasmine outside out Angular, you may have seen done being passed to the callback

it('..', function(done) {
  someAsyncAction().then(() => {
    expect(something).toBe(something);
    done();
  });
});

在这里,这是本地的Jasmine,在这里我们告诉Jasmine该测试应该延迟完成,直到我们调用done()为止.如果我们不致电done()而是这样做:

Here, this is native Jasmine, where we tell Jasmine that this test should delay completion until we call done(). If we didn't call done() and instead did this:

it('..', function() {
  someAsyncAction().then(() => {
    expect(something).toBe(something);
  });
});

测试将甚至在期望之前完成,因为承诺会在测试完成执行同步任务后的之后解决.

The test would complete even before the expectation, because the promise resolves after the test is finished executing the synchronous tasks.

在Angular中(在Jasmine环境中),当我们使用async时,Angular实际上会在幕后调用done.它将跟踪区域中的所有异步任务,当它们全部完成后,将在后台调用done.

With Angular (in a Jasmine environment), Angular will actually call done behind the scenes when we use async. It will keep track of all the asynchronous tasks in the Zone, and when they are all finished, done will be called behind the scenes.

在特定情况下使用TestBed配置,通常在需要compileComponents时会使用它.我很少遇到不得不以其他方式称呼它的情况

In your particular case with the TestBed configuration, you would use this generally when you want to compileComponents. I rarely run into a situation in which I would have to call it otherwise

beforeEach(async(() => {
   TestBed.configureTestingModule({
     declarations: [MyModule],
     schemas: [NO_ERRORS_SCHEMA],
   })
   .compileComponent().then(() => {
      fixture = TestBed.createComponent(TestComponent);
   });
}));

在测试使用templateUrl的组件时(如果您未使用webpack),则Angular需要发出XHR请求以获取模板,因此该组件的编译将是异步的.因此,我们应该等到问题解决后再继续测试.

When testing a component that uses templateUrl (if you are not using webpack), then Angular needs to make an XHR request to get the template, so the compilation of the component would be asynchronous. So we should wait until it resolves before continuing testing.

这篇关于Angular 2测试-异步函数调用-何时使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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