26. 開發與測試
function calculator() {
this.getDiscountPrice = function(totalPrice) {
if (totalPrice >= 200) {
return totalPrice * 0.8;
} else if (totalPrice >= 100 && totalPrice < 200) {
return totalPrice * 0.9
}
return totalPrice
}
}
describe('Calculator', function() {
describe('#GetDiscountPrice(totalPrice)', function() {
describe('if total price over 200, get 80% discount', function() {
it('should return 200 if total price is 250', function() {
var result = calculator.getDiscountPrice(250);
result.should.equal(200);
});
});
describe('if total price over 100 and less than 200, get 90% discount', function() {
it('should return 135 if total price is 150', function() {
var result = calculator.getDiscountPrice(150);
result.should.equal(135);
});
});
describe('if total price less than 100, no discount', function() {
it('should return 80 if total price is 80', function() {
var result = calculator.getDiscountPrice(80);
result.should.equal(80);
});
});
});
});
商業邏輯
測試程式
不容易理解,需花時間看 Code