kmock javascript 单元测试代码_javascript技巧
程序员文章站
2022-05-17 18:21:23
...
复制代码 代码如下:
(function () {
var KMock = window.KMock = function () {
}
KMock.prototype.setup = function (methodName) {
var instance = this;
instance[methodName] = {
returnAs: function (fn) {
instance["_" + methodName] = function () {
instance[methodName].invokeNum++;
fn.call();
}
},
isVerify: function (exceptNum) {
if (exceptNum != null) {
return exceptNum == instance[methodName].invokeNum;
}
else {
return instance[methodName].invokeNum > 0;
}
},
invoke: function () {
instance["_" + methodName].call();
},
invokeNum: 0
};
return instance[methodName];
}
})();
//自己写了个mock类,暂时是用来模拟函数,然后判断函数的调用次数,以下是用法
///
///
///
module("enter coin");
test("enter coin while game has not yet start", function () {
var flight = new KFlight();
var mock = new KMock();
mock.setup("draw").returnAs(function () {
});
flight.draw = mock.draw.invoke;
equal(flight.coinNum, 0);
equal(flight.gameState, 0);
flight.coinEnter();
equal(flight.coinNum, 0);
equal(flight.gameState, 1);
equal(mock.draw.isVerify(1), true);
});
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
相关文章
相关视频