我了解单元测试独立函数(如帮助程序类)的知识,但我如何处理非独立函数,通常在具有多个验证和结果的类文件中?
下面的示例显示了不同结果中的多个条件检查和响应。
- 我是否在我的测试用例中调用
valueCheck
and函数?proceedApiCheck
但是在测试中我不需要不同的场景或操作。(例如,setState / 导航) - 我是否在我的测试用例中
valueCheck
编写了一个新函数?proceedApiCheck
但这意味着我的代码中有两种不同的逻辑。有一天,如果我在应用程序中更改逻辑,我的测试用例不会失败,因为它指的是旧逻辑。
你们中的任何人都可以对此有所了解吗?
例子
export class Screen1 extends React.Component {
valueCheck = (value) => {
if(value === 'abc'){
this.setState({ isNavigating:true, transfer: true })
this.proceedApiCheck(value)
}
if(value === '123'){
this.setState({ isNavigating:true, transfer: false })
this.proceedApiCheck(value)
}
}
proceedApiCheck = async(value) =>{
let data
try{
data = await FirstApi(value);
this.setState(data)
}catch(){
this.navigateToScreen('Failure')
return;
}
switch(data.name){
case 'fake adidas':
this.navigateToScreen('Failure')
return;
case 'fake nike':
this.navigateToScreen('Failure')
return;
}
try{
const result = await secondApi(data.price);
switch(result.currency){
case 'EURO':
this.navigateToScreen('Euro')
case 'Pound':
this.navigateToScreen('Pound')
default:
this.navigateToScreen('Dollar')
}
}catch(){
this.navigateToScreen('Failure')
return;
}
}
}