【发布时间】:2014-02-27 01:18:17
【问题描述】:
假设我正在 Rspec 中为 Rails 应用程序编写规范,并且我正在删除用于减少规范中依赖项数量的方法:
# specs/account_statistics_spec.rb
describe AccountStatistics do
it "gets the percentage of users that are active in an account" do
account = Account.new()
account.stub_chain(:users, :size).and_return(80)
account.stub_chain(:users, :active, :size).and_return(20)
stats = AccountStatistics.new(account)
stats.percentage_active.should == 25
end
end
即使 Account#users 和 User#active 方法未在各自的类中定义,现在也可以通过 AccountStatistics 规范。
有什么好的方法可以捕捉到存根方法可能无法实现的事实?是否应该由集成测试来捕获未定义的方法?或者规范是否应该在存根之前检查方法是否已定义?
如果有人可以链接到任何深入讨论存根和模拟的好书/演示文稿,那就太好了:)
【问题讨论】:
标签: ruby-on-rails ruby rspec tdd stub