作为一个JavaScript的初学者,我很难理解如何“加载”我的代码,以便在应用程序之外使用单元测试进行测试。
在PHP中,我有一个自动加载器(composer),所以我可以引用我想要交互的类,并且它会自动加载,或者我可以使用
因此,其中一个类(存储在文件src/lib/App/Calculator.js中)...
在PHP中,我有一个自动加载器(composer),所以我可以引用我想要交互的类,并且它会自动加载,或者我可以使用
require_once
来包含一个物理文件,那个文件中的类或函数就可以被我使用。因此,其中一个类(存储在文件src/lib/App/Calculator.js中)...
App.Calculator = (function () {
var Calculator = function (onUpdateCallback) {
this.amountChange = 0.00;
this.amountDue = 0.00;
this.amountTendered = 0.00;
this.onUpdateCallback = onUpdateCallback;
};
Calculator.prototype = {
clear: function () {
this.amountTendered = 0.00;
this.calculate();
},
calculate: function () {
if (this.amountDue > 0) {
this.amountChange = Math.max(0, this.getAmountTendered() - this.amountDue);
} else {
this.amountChange = -(this.getAmountTendered() + this.amountDue);
}
if (typeof this.onUpdateCallback === 'function') {
this.onUpdateCallback(this);
}
},
getAmountTendered: function () {
return Number(this.amountTendered);
},
getChange: function () {
return this.amountChange;
},
setAmountTendered: function (amountTendered) {
this.amountTendered = parseFloat(amountTendered);
this.calculate();
},
setAmountDue: function (amountDue) {
this.amountDue = parseFloat(amountDue);
if (this.amountDue < 0) {
this.negative = true;
}
this.calculate();
}
};
return Calculator;
})();
我希望能够在应用程序之外,从命令行创建一个新的App.Calculator实例并进行测试。
从PHPUnit转到JS,我知道我缺乏很多理解,所以任何指导意见(有偏见或无偏见)都将不胜感激。
App.Calculator
,并对其进行所需的操作。 - Cerbrus./
或../
开头的相对路径。 - Herku