I'm using Ruby 2.2.1 and Rails 4.2.0
I'm adding test cases to cover a module. The module basically runs some QA checks on data pulled in from another system. The problem I'm having is that across test cases, an iteration inside the module is re-using the same object instead of the individual objects for the individual test cases.
Sample test cases:
...
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
...
Sample module:
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
Because the loop is re-using the object, the second test case never passes because the module is just re-evaluating the first trip
object. Is there a way to force it to instantiate a new object in the loop??