抵押计算器功能的单元测试

时间:2018-11-02 17:00:15

标签: javascript jestjs

我编写了此函数,该函数根据房屋成本,预付款,按揭期限(贷款期限)和年利率来计算按揭付款。我想测试calculatePayment函数,以确保输出正确。我选择了Jest,但其他任何测试工具都可以使用。如何为此功能编写测试?

function calculatePayment() {

    var houseCost = parseFloat(document.getElementById("houseCost").value);
    var downPayment = parseFloat(document.getElementById("downPayment").value);

    var termOfLoan = parseFloat(document.getElementById("termOfLoan").value);

    var annualInterestRate = parseFloat(document.getElementById("annualInterestRate").value);
    var principal = houseCost - downPayment;
    var percentageRate = annualInterestRate / 1200;
    var lengthOfLoan = 12 * termOfLoan;
    var monthlyPayment = (principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1)));
    monthlyPayment = monthlyPayment.toFixed(2);
    document.getElementById("payment").value = monthlyPayment;

};

此外,如果有更好的方法来重构它,我将不胜感激

1 个答案:

答案 0 :(得分:0)

单元测试对单元进行测试。现在,您拥有一个完整的功能。

将您的功能拆分为子功能,并测试这些功能和组合效果。

然后您就可以真正测试您的单元了。

这只是一个非常非常基本的示例。最大限度地使用单元测试套件,确保还测试了exeptions(例如,如果有人输入fizzlefup而不是数字,该怎么办)。

function getFloatFromInput(inputId) {
   return parseFloat(document.getElementById(inputId).value);
}
function getHouseCost() {
    return getFloatFromInput("houseCost");
}
function getDownPayment() {
    return getFloatFromInput("downPayment");
}
function getTermOfLoan() {
    return getFloatFromInput("termOfLoan");
}
function getAnnualInterestRate() {
    return getFloatFromInput("annualInterestRate");
}
function calculatePrincipal(houseCost, downPayment) {
    return houseCost - downPayment;
}
function calculatePercentageRate(annualInterestRate) {
    return annualInterestRate / 1200;
}
function calculateLengthOfloan(termOfLoan) {
    return 12 * termOfLoan;
}
function calculateMonthlyPayment(principal, percentageRate, lengthOfLoan) {
    return ((principal * percentageRate) / (1 - (Math.pow((1 + percentageRate) , lengthOfLoan * -1)))).toFixed(2);
}

function calculatePayment() {

    var houseCost = getHouseCost();
    var downPayment = getDownPayment();

    var termOfLoan = getTermOfLoan();

    var annualInterestRate = getAnnualInterestRate();
    var principal = calculatePrincipal(houseCost, downPayment);
    var percentageRate = calculatePercentageRate(annualInterestRate);
    var lengthOfLoan = calculateLengthOfloan(termOfLoan);
    var monthlyPayment = calculateMonthlyPayment(principal, percentageRate, lengthOfLoan);
    document.getElementById("payment").value = monthlyPayment;
};

function test() {
    var assertEquals = function(expected, actual) {
      if(expected === actual) {
        console.log(".");
        return;
      }
      throw new Error('Expecting ' + expected +' received '+ actual);
    }
    var tests = [];
    tests.push(function() {
        document.getElementById('houseCost').value = 1;
        var value = getFloatFromInput('houseCost');
        assertEquals(parseFloat("1"),value);
    });
    tests.push(function() {
        document.getElementById('houseCost').value = 1;
        var value = getHouseCost();
        assertEquals(parseFloat("1"),value);
    });
    tests.push(function() {
        document.getElementById('downPayment').value = 1;
        var value = getDownPayment();
        assertEquals(parseFloat("1"),value);
    });
    tests.push(function() {
        document.getElementById('termOfLoan').value = 1;
        var value = getTermOfLoan();
        assertEquals(parseFloat("1"),value);
    });
    tests.push(function() {
        document.getElementById('annualInterestRate').value = 1;
        var value = getAnnualInterestRate();
        assertEquals(parseFloat("1"),value);
    });
    tests.push(function() {
        var value = calculatePrincipal(10.5, 5.3);
        assertEquals(5.2, value);
    });
    tests.push(function() {
        var value = calculatePercentageRate(1200);
        assertEquals(1, value);
    });
    tests.push(function() {
        var value = calculateMonthlyPayment(1,1,1);
        assertEquals((2.0).toFixed(2), value);
    });
    tests.push(function() {
        document.getElementById('houseCost').value = 1;
        document.getElementById('downPayment').value = 0;
        document.getElementById('termOfLoan').value = 1;
        document.getElementById('annualInterestRate').value = 1;
        calculatePayment()
        assertEquals("0.08",document.getElementById('payment').value);
    });
    for(var c=0;c< tests.length;c++) {
         tests[c]();
    }
    
}
test();
<label for="houseCost">houseCost</label><input type="text" id="houseCost"><BR/>
<label for="downPayment">downPayment</label><input type="text" id="downPayment"><BR/>
<label for="termOfLoan">termOfLoan</label><input type="text" id="termOfLoan"><BR/>
<label for="annualInterestRate">annualInterestRate</label><input type="text" id="annualInterestRate"><BR/>
<BR/>
<label for=""></label>
<input type="text" id="payment">