茉莉花:当测试返回SPEC不能预期?

我有此测试可以成功返回“ SPEC HAS NOT EXPECTATION”,您有什么主意吗?也许我没有遵循最佳实践;

 editComponent has been declared into the Test.bed  into the declaration[]

it('test',fakeAsync(() => {
        fixture.whenStable().then(() => {
            editComponent = component.editComponent;
            editComponent.ruleForm = new FormGroup({
                title: new FormControl('test field'),cause: new FormControl('test field'),});
            fixture.detectChanges();
            spyOn(ruleEditComponent,'updateRule').withArgs(activeIssueId);
            component.saveRuleDetails(activeIssueId);
            expect(editComponent.createRule).toHaveBeenCalledWith(activeIssueId);
        });

    }));

当测试返回SPEC不能预期吗?

qijiannian 回答:茉莉花:当测试返回SPEC不能预期?

fixture.whenStable返回一个Promise,其结果被异步处理。因此,单元测试甚至在调用任何expect之前就结束了。

fakeAsync必须与tickflush一起使用,以便以同步方式模拟异步处理。尝试如下重写测试。

it('test',fakeAsync(() => {
    fixture.whenStable().then(() => {
        editComponent = component.editComponent;
        editComponent.ruleForm = new FormGroup({
            title: new FormControl('test field'),cause: new FormControl('test field'),});
        fixture.detectChanges();
        spyOn(ruleEditComponent,'updateRule').withArgs(activeIssueId);
        component.saveRuleDetails(activeIssueId);        
    });
    tick();
    expect(editComponent.createRule).toHaveBeenCalledWith(activeIssueId);
}));

或者,您可以按以下方式使用done函数。

it('test',(done) => {
    fixture.whenStable().then(() => {
        editComponent = component.editComponent;
        editComponent.ruleForm = new FormGroup({
            title: new FormControl('test field'),'updateRule').withArgs(activeIssueId);
        component.saveRuleDetails(activeIssueId);        
        expect(editComponent.createRule).toHaveBeenCalledWith(activeIssueId);
        done();
    });        
}));
本文链接:https://www.f2er.com/3143446.html

大家都在问