Jasmine:当测试 return 规格不符合预期时?

Jasmine: When a test return SPEC HAS NOT EXPECTATION?

我有这个 returns "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);
        });

    }));

当测试 return 规格不符合预期时?

fixture.whenStable returns a 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'),
            cause: new FormControl('test field'),
        });
        fixture.detectChanges();
        spyOn(ruleEditComponent, 'updateRule').withArgs(activeIssueId);
        component.saveRuleDetails(activeIssueId);        
        expect(editComponent.createRule).toHaveBeenCalledWith(activeIssueId);
        done();
    });        
}));