Kmockjavascript unit test code _ javascript skills

Source: Internet
Author: User
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);

});
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.