[英]How to test js factory function using mocha & chai
我正在嘗試測試我的 DOM 項目,所以它應該確保成本為 2.75,短信為 0.75。 它返回一個斷言錯誤,表明預期 2.75 等於未定義。 我需要幫助來訪問呼叫和短信的正確值。 這是我的工廠 function
var callCost = 0;
var smsCost = 0;
var totalCost = 0;
const warning = 30;
const critical = 50;
function getCall() {
return callCost;
}
function getSms() {
return smsCost;
}
function getTotal() {
totalCost = callCost + smsCost;
return totalCost;
}
function radioButtons(selectedBill) {
if (selectedBill === "call") {
callCost += 2.75;
} else if (selectedBill === "sms") {
smsCost += 0.75;
}
}
function totalClassName() {
if (getTotal() >= warning && getTotal() < critical) {
return "warning";
} else if (getTotal() >= critical) {
return "critical";
}
}
return {
getCall,
getSms,
getTotal,
radioButtons,
totalClassName
}
}
describe('The radio-bill function', function(){
it('Should be able to add call at 2.75', function(){
var itemType = RadioBill();
itemType.radioButtons("call");
assert.equal(2.75, itemType.radioButtons("call"))
})
})
你只需要改變你的assert
行來讓你的測試工作。
var itemType = RadioBill();
itemType.radioButtons("call");
assert.equal(itemType.getCall(), 2.75);
在這里,首先要注意的是 arguments 在斷言調用中的順序確實很重要。 第一個參數是實際值,第二個參數是期望值。 通常,但不總是,實際值將是操作的結果,而預期值將是恆定的。
第二點是在您的代碼中radioButtons
單選按鈕不返回值,它只是更改內部 state 變量的值。 但是已經有 function getCall
來獲取該值,這就是assert
行正在檢查的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.