簡體   English   中英

用Mocha測試Number.Prototype

[英]Testing Number.prototype with Mocha

假設我在Java的Number原型上有一個函數,如下所示:

控制器/ index.js

Number.prototype.adder = function(num) {
    return this+num;
}
module.exports = Number;

但是,以下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();
    });

});

錯誤:TypeError:未定義不是函數

我很確定問題是由module.exports = Number部分引起的。 所以基本上我的問題是-我如何在Number.prototype中導出一個函數以使其如上所述可測試。

您的函數稱為加法器,因此您應該

expect(one.adder(5)).to.equal(9);

代替

expect(one.CustomAdder(5)).to.equal(9);    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM