[英]Testing Number.prototype with Mocha
Let's say I have a function on Javascript's Number prototype as follows: 假设我在Java的Number原型上有一个函数,如下所示:
controllers/index.js 控制器/ index.js
Number.prototype.adder = function(num) {
return this+num;
}
module.exports = Number;
However, the following Mocha/Chai tests are failing 但是,以下Mocha / Chai测试失败
var expect= require("chai").expect;
var CustomAdder= require("../controller/index.js");
describe("adder", function () {
var one= 4;
var two= 5;
it("should add 4 and 5 to 9", function(done){
expect(one.CustomAdder(5)).to.equal(9);
done();
});
it("should not add 5 and 6 to 11", function(done){
expect(two.CustomAdder(6)).to.not.equal(12);
done();
});
});
Error: TypeError: undefined is not a function 错误:TypeError:未定义不是函数
I am pretty sure that the problem is caused by the module.exports = Number part. 我很确定问题是由module.exports = Number部分引起的。 So basically my question is- how do I export a function in Number.prototype to make it testable as above.
所以基本上我的问题是-我如何在Number.prototype中导出一个函数以使其如上所述可测试。
Your function is called adder, so you should do 您的函数称为加法器,因此您应该
expect(one.adder(5)).to.equal(9);
Instead of 代替
expect(one.CustomAdder(5)).to.equal(9);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.