简体   繁体   English

Jasmine:当测试返回 SPEC 没有预期?

[英]Jasmine: When a test return SPEC HAS NOT EXPECTATION?

i have this test that returns "SPEC HAS NOT EXPECTATION" without fails, do you have any idea?我有这个测试返回“SPEC HAS NOT EXPECTATION”没有失败,你知道吗? maybe i am not following the best practice;也许我没有遵循最佳实践;

 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);
        });

    }));

When a test return SPEC HAS NOT EXPECTATION?当测试返回 SPEC 没有预期?

fixture.whenStable returns a Promise and it's result is handled asynchronously. fixture.whenStable返回一个Promise并且它的结果是异步处理的。 Therefore the unit test ends even before any expect is invoked.因此,单元测试甚至在调用任何expect之前就结束了。

fakeAsync must be used together with tick or flush in order to simulate asynchronous processing in a synchronous way. fakeAsync必须与tickflush一起使用,以便以同步的方式模拟异步处理。 Try to rewrite your test as follows.尝试如下重写您的测试。

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);
}));

Alternatively you can use the done function as follows.或者,您可以使用done的 function,如下所示。

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();
    });        
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM