<分区>
我有一个像这样的 util 函数:
export const selectOperator = (operator, n1, n2) => {
let result;
switch(operator) {
case '+':
result = n1 + n2;
console.log(result)
case "-":
result = n1 - n2;
case "*":
result = n1 * n2;
case "/":
result = n1 / n2;
}
return result;
}
我正在尝试这样测试:
it('should select the right case based on the operator', () => {
const operators = ['+', '/', '-', '*']
const numA = 10
const numB = 5
operators.forEach(el => {
if(el == '+'){
//selectOperator(el, numA, numB).toEqual(12)
console.log(selectOperator('+', 10, 5))
}
if(el == '-'){
//selectOperator(el, numA, numB).toEqual(12)
//console.log(selectOperator(el, numA, numB))
}
if(el == '/'){
//selectOperator(el, numA, numB).toEqual(12)
//console.log(selectOperator(el, numA, numB))
}
if(el == '*'){
//selectOperator(el, numA, numB).toEqual(12)
//console.log(selectOperator(el, numA, numB))
}
})
})
util 函数中的控制台返回 15,这是正确的,但是测试中的控制台返回 2(似乎是除以 10/5)