【发布时间】:2013-01-08 00:30:35
【问题描述】:
我有一个重试块
def my_method
app_instances = []
attempts = 0
begin
app_instances = fetch_and_rescan_app_instances(page_n, policy_id, policy_cpath)
rescue Exception
attempts += 1
retry unless attempts > 2
raise Exception
end
page_n += 1
end
fetch_and_rescan_app_instances 访问网络,因此可以抛出异常。
我想编写一个 rspec 测试,它第一次抛出异常并且第二次调用它时不抛出异常,所以我可以测试它是否第二次不抛出异常,my_method 不会t 抛出异常。
我知道我可以 stub(:fetch_and_rescan_app_instances).and_return(1,3) 并且第一次返回 1,第二次返回 3,但我不知道如何第一次抛出异常并第二次返回。
【问题讨论】:
标签: ruby-on-rails ruby rspec