繁体   English   中英

有两个条件的 Karma 单元测试 IF

[英]Karma unit testing IF with two conditions

我正在编写单元测试,我有点坚持这种方法。 我有从 formGroup 获取名称的 onClickCheck 方法(这部分我在测试中介绍),然后检查 data.action 是否等于 NEW 以及 name 是否存在,然后执行某些操作。

onClickCheck() {
    const name = this.formGroup.get('name').value;


    if (this.data.action === 'NEW' && name) {
        this.service
            .doSomething(name)
            .pipe(take(1))
            .subscribe((result: any) => {
                this.someVar = result.data.attributes;

                if (this.someVar.isDeleted) {
                    this.confirmationDialog(dialogData);
                }
            });
    }
}

在我的测试中,我尝试这样做,但出现错误:expect(spy).toHaveBeenCalled() 但调用次数为 0。

const doSomethingSpy = spyOn(
            service,
            'doSomething'
        ).and.callThrough();

        component.formGroup.setValue({
            name: 'Test',
            lastName: 'TT2'
        });

        component.data= {
            action: 'NEW',
            item: null
        };

        const name = component.formGroup.get('name').value;

        component.onClickCheck();
        fixture.detectChanges();
        expect(doSomethingSpy ).toHaveBeenCalled();

如何在这个双重条件 IF 中测试代码?

const name = component.formGroup.get('name ').value;

可能是因为您在测试中正在寻找“名称”而不是“名称”吗?

暂无
暂无

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

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