使用fakeAsync()而不是Jasmine完成()测试Angular 2中的基本导航

时间:2016-06-16 21:21:16

标签: unit-testing angular jasmine angular2-routing gherkin

我目前正在使用Gherkin步骤和Jasmine async done()进行基本导航的工作测试:

User_Navigates_To_URL(urlPath: string) {
    return this.router.navigateByUrl(urlPath);
}

User_Will_Be_On_URL(expectedPath: string) {
    expect(this.location.path()).toBe(expectedPath);
}

it('Scenario: User should be able to navigate', (done) => {
    When.User_Navigates_To_URL('/associates/your-team').then(() => {
        Then.User_Will_Be_On_URL('/associates/your-team');
    });
    done();
});

但我想要完成的是使用fakeAsync而不是jasmine的done()方法来编写此测试。这样,所有异步操作都将在区域中解析,我不必将断言步骤嵌套为promise的回调。因此,我试图这样做:

it('Scenario: User should be able to navigate', <any>fakeAsync(() => {
    When.User_Navigates_To_URL('/associates/your-team');
    tick();
    Then.User_Will_Be_On_URL('/associates/your-team');
}));

经过数周的研究,我发现唯一有用的是这个问题:Does fakeAsync guarantee promise completion after tick/flushMicroservice。但即使我试图在他的代码片段中实现我的导航承诺,它也永远无法解决。

User_Navigates_To_URL(urlPath: string) {
    let currNav:Promise<Router> = this.router.navigateByUrl(urlPath);

    let handleNavigation = function (p:Promise<Router>) {
        p.then(() => {
            console.log('navigation complete')
        });
    };

    let p = Promise.resolve(currNav);

    handleNavigation(p);
    tick();
}

这是我在这里的第一个问题,所以如果我的问题令人困惑或者我需要提供更多细节,请告诉我。

0 个答案:

没有答案