Home >Web Front-end >JS Tutorial >kmock javascript unit test code_javascript skills

kmock javascript unit test code_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 18:10:591250browse
Copy code The code is as follows:

(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];
}


})();

//I wrote a mock class myself, It is temporarily used to simulate functions and then determine the number of calls to the function. The following is the usage

///  
///
///


module("enter coin");

test("enter coin while game has not yet started", 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);

});
Statement:
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