kmock javascript 单元测试代码

时间:2021-05-26

复制代码 代码如下:
(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类,暂时是用来模拟函数,然后判断函数的调用次数,以下是用法
<BR><DIV class=cnblogs_Highlighter><PRE class=brush:javascript>/// <reference path="Flight.js" />
/// <reference path="http://demo.jb51.net/jslib/qunit/qunit.js" />
/// <reference path="KMock.js" />


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);

});

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章