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