RSpec keeps instance variable between post requests

Viewed 635

In a specific test, I want to add multiples items (package_sets) in some orders from my site.

To test it, I do as follow

  context "with multiple package sets from different shops" do

    let(:package_sets) { FactoryGirl.create_list(:package_set, 3) }

    it "adds package sets to multiple orders" do
      package_sets.each do |package_set|
        post :create, {'package_set_id': package_set.id}
        expect_json(success: true)
      end
      expect(Order.count).to eq(3)
    end

  end

In the real world, each time I call post :create it will assign the package set to a different order ; the total number of orders should be 3.

In my controller I memoize the order within a method like this

def order
   @order ||= cart_manager.order(shop: package_set.shop, call_api: false)
end

Because it's 3 different requests, I was expecting the test to reload this method too without memoization, but it keeps the @order memoized between the the post requests which makes the test fails.

How do you prevent this ?

2 Answers
Related