Copy Code code 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];
}
})();
wrote a mock class for the time being to simulate a function, and then to determine how many times the function was invoked, 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);
});