我正在使用Ruby 2.2.1和Rails 4.2.0Rspec测试重用对象
我正在添加测试用例来覆盖模块。该模块基本上对从另一个系统提取的数据执行一些QA检查。我遇到的问题是跨越测试用例,模块内部的迭代重复使用同一个对象,而不是各个测试用例的单个对象。
样品测试情况:
...
it "should add issue case 1" do
trip = FactoryGirl.build(:trip, :case_1)
p trip.object_id # 7...8660
subject.do_qa(trip)
expect(trip.issue_1).not_to be_nil
end
it "should add issue case 2" do
trip = FactoryGirl.build(:trip, :case_2)
p trip.object_id # 7...2780
subject.do_qa(trip)
expect(trip.issue_2).not_to be_nil
end
...
样本模块:
module Qa
...
def self.do_qa(trips)
p trips.object_id # Same is the object id in the calling test case
@trips ||= Array.wrap(trips)
@trips.each do |t|
p t.object_id # Always the object id from the first test case!
... # Checks for case 1 and case 2
end
end
...
end
由于环路重新使用对象,第二测试情况下从未通过,因为该模块只是重新评估第一个trip
对象。有没有办法强制它在循环中实例化一个新对象?
有一个可怕的方法,可能有更好的方法,但我们需要看到更多的代码。 'Qa'中的其他方法是指'@ trips'?什么课包括'Qa'?测试中的“主题”是什么? ([RSpec的主题大多是一件坏事。](http://stackoverflow.com/questions/38437162/whats-the-difference-between-rspecs-subject-and-let-when-should-they-be-used) ) –