Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何测试返回承诺或拒绝的函数-茉莉花和业力_Unit Testing_Jasmine_Karma Jasmine_Karma Coverage_Jasmine2.0 - Fatal编程技术网

Unit testing 如何测试返回承诺或拒绝的函数-茉莉花和业力

Unit testing 如何测试返回承诺或拒绝的函数-茉莉花和业力,unit-testing,jasmine,karma-jasmine,karma-coverage,jasmine2.0,Unit Testing,Jasmine,Karma Jasmine,Karma Coverage,Jasmine2.0,我需要覆盖函数的100%测试,它返回一个返回新承诺((解析,拒绝)这是完整的函数 saveData(): Promise < boolean > { return new Promise < boolean > ((resolve, reject) => { this.legaloneService.saveFinancialData(this.financialModel).subscribe( m => {

我需要覆盖函数的100%测试,它返回一个
返回新承诺((解析,拒绝)
这是完整的函数

saveData(): Promise < boolean > {
    return new Promise < boolean > ((resolve, reject) => {
        this.legaloneService.saveFinancialData(this.financialModel).subscribe(
            m => {
                if (!m.Success) {
                    const mapFiels: {
                        [id: string]: string
                    } = {};
                    mapFiels['accountName'] = 'Nome da conta';
                    mapFiels['bankId'] = 'Banco';
                    mapFiels['agency'] = 'Agência';
                    mapFiels['accountNumber'] = 'Conta';
                    this.functionsService.displayErrorFromAPII(m, mapFiels);
                }
                resolve(m.Success);
            },
            error => {
                const msg = 'Ocorreu uma falha ao salvar os dados financeiros';
                this.functionsService.cathError(error, msg);
                reject(msg);
            }
        );
    });
}
这是我目前的报道:


在您的测试中,您模拟了测试中的方法
saveData
,实际实现将不会涉及,代码覆盖率也不会提高。因此,您应该从测试中删除以下语句

spyOn(component, 'saveData').and.returnValue(Promise.resolve(true));
您应该模拟方法
legaloneService.saveFinancialData
,因为这是一个单元测试。因为
saveData
返回一个
Promise
,您可以使用
done
回调函数

要获得完整的代码覆盖率,您需要以下两个测试

import { of, throwError } from 'rxjs';
...

it('#saveData should display error when financial data cannot be saved', (done) => {    
    const saveFinancialDataResult = ?; // replace ? with expected !Success result    
    spyOn(legaloneService, 'saveFinancialData').and.returnValue(of(saveFinancialDataResult));
    spyOn(funcService, 'displayErrorFromAPII').and.stub();

    component.saveData()
        .then(r => {
            expect(funcService.displayErrorFromAPII).toHaveBeenCalled();    
            done();        
        })
        .catch(e => fail(e));
});

it('#saveData should catch error when error occurs', (done) => {    
    spyOn(legaloneService, 'saveFinancialData').and.returnValue(throwError('server error'));
    spyOn(funcService, 'cathError').and.stub();

    component.saveData()
        .then(r => fail('should have been rejected'))
        .catch(e => {
            expect(functionsService.cathError).toHaveBeenCalled();
            done();
        });
});
有关Angular和Jasmine的不同测试策略的详细信息,请咨询和

import { of, throwError } from 'rxjs';
...

it('#saveData should display error when financial data cannot be saved', (done) => {    
    const saveFinancialDataResult = ?; // replace ? with expected !Success result    
    spyOn(legaloneService, 'saveFinancialData').and.returnValue(of(saveFinancialDataResult));
    spyOn(funcService, 'displayErrorFromAPII').and.stub();

    component.saveData()
        .then(r => {
            expect(funcService.displayErrorFromAPII).toHaveBeenCalled();    
            done();        
        })
        .catch(e => fail(e));
});

it('#saveData should catch error when error occurs', (done) => {    
    spyOn(legaloneService, 'saveFinancialData').and.returnValue(throwError('server error'));
    spyOn(funcService, 'cathError').and.stub();

    component.saveData()
        .then(r => fail('should have been rejected'))
        .catch(e => {
            expect(functionsService.cathError).toHaveBeenCalled();
            done();
        });
});