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
it "should add issue case 2" do
trip = FactoryGirl.build(:trip, :case_2)
p trip.object_id # 7...2780
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
The answer to this ended up being my lack of full understanding of Ruby vs. other languages. Basically, even though a Module isn't defined as a class, it's still a class behind the scenes and the instance variables stay set between runs. This hasn't been an issue in my application because nothing has relied on them being cleared out between method calls.
Case 1 and Case 2 in the original question are actually using the same instance of the
Qa class, so of course the instance variable is going to still contain the original
trip object since I was using the
I resolved it by adding a
before(:each) block to the beginning of the relevant context with
Qa.instance_variable_set(:@trips, ) to ensure that the variable is starting in a clean state.