DEV: Stubbing "Rails.env.production?" introduces a lot of flakiness. (#13188)

DEV: Stubbing “Rails.env.production?” introduces a lot of flakiness. (#13188)

We need to be careful when stubbing this method. SessionController#become won’t be defined if production is set to true, so if these tests run first, calling #sign_in will fail for other tests.

Calling sign_in before stubbing guarantees the method is defined because the check happens when the class is loaded.

diff --git a/spec/requests/qunit_controller_spec.rb b/spec/requests/qunit_controller_spec.rb
index 36d91d1..a72b693 100644
--- a/spec/requests/qunit_controller_spec.rb
+++ b/spec/requests/qunit_controller_spec.rb
@@ -33,16 +33,21 @@ describe QunitController do
 
     context "non-admin users on production" do
       before do
+        # We need to call sign_in before stubbing the method because SessionController#become
+        # checks for the current env when the file is loaded.
+        # We need to make sure become is called once before stubbing, or the method
+        # wont'be available for future tests if this one runs first.
+        sign_in(Fabricate(:user))
         Rails.env.stubs(:production?).returns(true)
       end
 
-      it "anons cannot see the page" do
+      it "regular users cannot see the page" do
         get '/theme-qunit'
         expect(response.status).to eq(404)
       end
 
-      it "regular users cannot see the page" do
-        sign_in(Fabricate(:user))
+      it "anons cannot see the page" do
+        sign_out
         get '/theme-qunit'
         expect(response.status).to eq(404)
       end

GitHub sha: 9b4a873c

This commit appears in #13188 which was approved by eviltrout. It was merged by romanrizzi.