【发布时间】:2014-10-15 04:02:14
【问题描述】:
我正在构建一个投资工具并希望确保它计算出正确的到期金额。
这里是一些源代码:
def maturity_amount
if cumulative
amount = initial_deposit * (1 + (rate_of_interest)/4 ) ** (term * 4)
amount.round
else
initial_deposit
end
end
所以我写了一个这样的测试:
it "can calculate interest earned" do
investment = FactoryGirl.build(:investment, initial_deposit: 250000, rate_of_interest: 9.5, cumulative: true)
expect(investment.maturity_amount).to eq(331335)
end
您在这里看到我将结果硬编码到测试中。这会使测试变得脆弱吗?此外,如果不每次都更新最终结果,我似乎无法轻易改变我对测试的输入。
那么有没有更好的方法来编写测试并且仍然有信心做出正确的计算?我应该嘲讽吗?还是我不应该首先编写这样的测试?如果有,为什么?
【问题讨论】:
标签: ruby unit-testing rspec tdd