kmock javascript 유닛 테스트 코드

1627 단어 유닛 테스트

(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 , , ,
<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);

});

좋은 웹페이지 즐겨찾기