(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类,暂时是用来模拟函数,然后判断函数的调用次数,以下是用法
/// <reference path="Flight.js"></reference> <br>/// <reference path="http://demo.jb51.net/jslib/qunit/qunit.js"></reference> <br>/// <reference path="KMock.js"></reference> <br><br><br>module("enter coin"); <br><br>test("enter coin while game has not yet start", function () { <br>var flight = new KFlight(); <br><br>var mock = new KMock(); <br>mock.setup("draw").returnAs(function () { <br>}); <br><br>flight.draw = mock.draw.invoke; <br><br><br>equal(flight.coinNum, 0); <br>equal(flight.gameState, 0); <br>flight.coinEnter(); <br>equal(flight.coinNum, 0); <br>equal(flight.gameState, 1); <br><br><br>equal(mock.draw.isVerify(1), true); <br><br>});<br>
Salin selepas log masuk