【发布时间】:2016-10-05 11:04:45
【问题描述】:
我想改进一种便捷方法的规范,该方法返回一组用户,这些用户的购买将于明天到期,并将使用某种支付方式(忽略其他支付方式)进行支付。
规格大致如下:
it "doesn't return users without purchases being due tomorrow" do
# create user and associated records that don't fit the time condition
expect(subject).to eq([])
end
context "with users who have purchases due tomorrow" do
it "returns users with $CERTAIN_PAYMENT_METHOD" do
# create different users with different payment methods,
# all matching the time condition.
expect(subject).to eq([user_1, user_2])
end
it "doesn't return users without $CERTAIN_PAYMENT_METHOD" do
# create user with credit card,
# matching the time condition.
expect(subject).to eq([])
end
end
我在这里看到了三种可能的方法:
- 上面使用的方法:设置记录的任意组合,并期望特定于单个条件的事物,始终断言整个数组。设置可能冗长且重复。
- 设置任意组合,并期望在返回的数组中包含/排除事物。这些“软期望”很好读,但很容易出错。
- 设置(几乎)所有记录组合,并有一个单一的期望。这不会产生重叠,但也会让开发人员对为什么这个方法返回这个特定的数组一无所知。
所有方法都有其缺点,我想知道是否有最佳实践来测试这些方法?
【问题讨论】:
-
您也可以发布您正在测试的方法吗?乍一看您的规范,我建议您将原始方法分解成更小的部分并进行测试。
标签: ruby-on-rails ruby testing rspec