如何在 Jasmine 中测试空函数?

How to test empty function in Jasmine?

我正在用打字稿创建一个工具模块,我有一个父 class BaseTool 和多个子 class 工具 A 和 ToolB。

在 BaseTool class 中,大多数方法都受到保护,子 class 正在覆盖它们。

代码基础工具class:

Class BaseTool {

protected doSelection() {
 //some code
}
}

代码工具A class(这是只读的class):

Class ToolA extends BaseTool {

protected doSelection() {} //<-- This function must be empty, and i don't know, how to test?
}

代码工具 B class(这是另一个 class 函数定义):

Class ToolB extends BaseTool {

protected doSelection() {
//super()
//code
} //<-- This function is not empty and i write all the desired test cases (no problem here)
}

如何为只读 class (ToolA) 受保护的无主体方法编写测试用例?

对同一个空函数使用同一个引用。 var emptyFunction = 函数 {}; 然后使用 vm.emptyFunction = angular.isFunction(回调)?回调:空函数; 然后测试 期望(vm.emptyFunction).toEqual(emptyFunction);

我使用 Function.prototype.toString()

创建了一个实用函数来测试评论中建议的空函数

function isEmpty(f) {
    // Get content between first { and last }
    const m = f.toString().match(/\{([\s\S]*)\}/m);
    // Strip comments
    const l = m && m[1].replace(/^\s*\/\/.*$/mg, '').trim();
    if (l.length === 0) {
        return true;
    } else {
        return false;
    }
};

function doSeatMouseClick(_event, _seat) { 
 //do nothing, read only
}

var bool = isEmpty(doSeatMouseClick);
console.log(bool)