有时,我编写的功能在某些情况下应该什么都不做。他们不做任何事情不仅可以接受,而且实际上是必需的行为。是否有模式来测试函数以确保它没有副作用?
作为一个例子,我目前正在测试一个如下所示的函数:
function onRowOfIconsClicked(iconName) {
if (iconName === 'new') {
doSomeAwesomeStuffImAlreadyTesting();
}
// We haven't implemented the other icons yet, so for now,
// do nothing and don't throw an error if a different icon
// is clicked
}
我如何编写一个测试用例来断言当前要求当onRowOfIconsClicked
使用除iconName
以外的"new"
调用时,该函数什么都不做?
答案 0 :(得分:-1)
所有JavaScript函数都有一个返回值,即使返回值为undefined
,因为没有用于指定返回值的显式return
语句。
您可以做的是测试如果使用iconName
以外的'new'
参数调用该函数,则测试函数是否返回undefined
。因为undefined === undefined
等会通过测试。