Home > Web Front-end > JS Tutorial > body text

kmock javascript 单元测试代码_javascript技巧

WBOY
Release: 2016-05-16 18:10:59
Original
1056 people have browsed it
复制代码 代码如下:

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

});
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!