Kmock JavaScript Unit test Code _javascript Tips

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

});
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.