Unit testing 如何测试返回承诺或拒绝的函数-茉莉花和业力
我需要覆盖函数的100%测试,它返回一个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 => {
返回新承诺((解析,拒绝)
这是完整的函数
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();
});
});