我有2个简单的功能。第一个函数X接收数字或字符串。如果它接收到一个数字,则返回其双精度数,如果它接收到一个字符串,则我调用另一个函数Y。如何测试函数X在接收到字符串作为参数时是否调用函数Y?
function X(arg) {
if (typeof (arg) === 'String') Y(arg)
else return arg * 2
}
function Y(arg) {
return 'Got empty string'
}Run Code Online (Sandbox Code Playgroud)
我想在测试中做什么
describe('A function X that checks data type', function() {
it('should call function Y is argument is a string', function() {
let arg = arguments[0]
expect(Y is called if typeof(arg)).toEqual('string')
})
})Run Code Online (Sandbox Code Playgroud)
对于这些类型的问题的更一般的答案,如果“如果Y,则执行X”会很棒。谢谢 :)