Javascript actually requires unit tests, but does qmock always feel bad? Or there is a problem with the new and old versions of the document. For now, I only need to judge the number of function calls and write a kmock.
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 (effectnum ){
If (distinct tnum! = Null ){
Return repeated tnum = instance [methodName]. invokeNum;
}
Else {
Return instance [methodName]. invokeNum> 0;
}
},
Invoke: function (){
Instance ["_" + methodName]. call ();
},
InvokeNum: 0
};
Return instance [methodName];
}
})();
// Write a mock class by yourself. It is used to simulate a function and determine the number of function calls. The usage is as follows:
///
///
///
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);
});