Use stub_rails_env for Rails.env stubbing

RailsHelpers.stub_rails_env takes care of
stubbing Rails.env
This commit is contained in:
Ash McKenzie 2019-09-04 11:57:25 +10:00
parent 0fca70a40f
commit 124bad7bd8
No known key found for this signature in database
GPG Key ID: A1253B4953E8D3E6
15 changed files with 25 additions and 45 deletions

View File

@ -58,7 +58,7 @@ describe 'Help Pages' do
before do before do
stub_application_setting(version_check_enabled: true) stub_application_setting(version_check_enabled: true)
allow(Rails.env).to receive(:production?).and_return(true) stub_rails_env('production')
allow(VersionCheck).to receive(:url).and_return('/version-check-url') allow(VersionCheck).to receive(:url).and_return('/version-check-url')
sign_in(create(:user)) sign_in(create(:user))

View File

@ -37,7 +37,7 @@ describe 'User can display performance bar', :js do
shared_examples 'performance bar is enabled by default in development' do shared_examples 'performance bar is enabled by default in development' do
before do before do
allow(Rails.env).to receive(:development?).and_return(true) stub_rails_env('development')
end end
it 'shows the performance bar by default' do it 'shows the performance bar by default' do

View File

@ -60,20 +60,19 @@ describe IconsHelper do
non_existing = 'non_existing_icon_sprite' non_existing = 'non_existing_icon_sprite'
it 'raises in development mode' do it 'raises in development mode' do
allow(Rails.env).to receive(:development?).and_return(true) stub_rails_env('development')
expect { sprite_icon(non_existing) }.to raise_error(ArgumentError, /is not a known icon/) expect { sprite_icon(non_existing) }.to raise_error(ArgumentError, /is not a known icon/)
end end
it 'raises in test mode' do it 'raises in test mode' do
allow(Rails.env).to receive(:test?).and_return(true) stub_rails_env('test')
expect { sprite_icon(non_existing) }.to raise_error(ArgumentError, /is not a known icon/) expect { sprite_icon(non_existing) }.to raise_error(ArgumentError, /is not a known icon/)
end end
it 'does not raise in production mode' do it 'does not raise in production mode' do
allow(Rails.env).to receive(:test?).and_return(false) stub_rails_env('production')
allow(Rails.env).to receive(:development?).and_return(false)
expect { sprite_icon(non_existing) }.not_to raise_error expect { sprite_icon(non_existing) }.not_to raise_error
end end

View File

@ -3,7 +3,7 @@ require 'spec_helper'
describe VersionCheckHelper do describe VersionCheckHelper do
describe '#version_status_badge' do describe '#version_status_badge' do
it 'returns nil if not dev environment and not enabled' do it 'returns nil if not dev environment and not enabled' do
allow(Rails.env).to receive(:production?) { false } stub_rails_env('development')
allow(Gitlab::CurrentSettings.current_application_settings).to receive(:version_check_enabled) { false } allow(Gitlab::CurrentSettings.current_application_settings).to receive(:version_check_enabled) { false }
expect(helper.version_status_badge).to be(nil) expect(helper.version_status_badge).to be(nil)
@ -11,7 +11,7 @@ describe VersionCheckHelper do
context 'when production and enabled' do context 'when production and enabled' do
before do before do
allow(Rails.env).to receive(:production?) { true } stub_rails_env('production')
allow(Gitlab::CurrentSettings.current_application_settings).to receive(:version_check_enabled) { true } allow(Gitlab::CurrentSettings.current_application_settings).to receive(:version_check_enabled) { true }
allow(VersionCheck).to receive(:url) { 'https://version.host.com/check.svg?gitlab_info=xxx' } allow(VersionCheck).to receive(:url) { 'https://version.host.com/check.svg?gitlab_info=xxx' }
end end

View File

@ -1283,33 +1283,19 @@ describe Gitlab::Database::MigrationHelpers do
describe '#perform_background_migration_inline?' do describe '#perform_background_migration_inline?' do
it 'returns true in a test environment' do it 'returns true in a test environment' do
allow(Rails.env) stub_rails_env('test')
.to receive(:test?)
.and_return(true)
expect(model.perform_background_migration_inline?).to eq(true) expect(model.perform_background_migration_inline?).to eq(true)
end end
it 'returns true in a development environment' do it 'returns true in a development environment' do
allow(Rails.env) stub_rails_env('development')
.to receive(:test?)
.and_return(false)
allow(Rails.env)
.to receive(:development?)
.and_return(true)
expect(model.perform_background_migration_inline?).to eq(true) expect(model.perform_background_migration_inline?).to eq(true)
end end
it 'returns false in a production environment' do it 'returns false in a production environment' do
allow(Rails.env) stub_rails_env('production')
.to receive(:test?)
.and_return(false)
allow(Rails.env)
.to receive(:development?)
.and_return(false)
expect(model.perform_background_migration_inline?).to eq(false) expect(model.perform_background_migration_inline?).to eq(false)
end end

View File

@ -1,8 +1,6 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::Favicon, :request_store do RSpec.describe Gitlab::Favicon, :request_store do
include RailsHelpers
describe '.main' do describe '.main' do
it 'defaults to favicon.png' do it 'defaults to favicon.png' do
stub_rails_env('production') stub_rails_env('production')

View File

@ -265,7 +265,7 @@ describe Gitlab::GitalyClient do
context 'in production and when RequestStore is enabled', :request_store do context 'in production and when RequestStore is enabled', :request_store do
before do before do
allow(Rails.env).to receive(:production?).and_return(true) stub_rails_env('production')
end end
context 'when the maximum number of calls is enforced by a feature flag' do context 'when the maximum number of calls is enforced by a feature flag' do

View File

@ -86,9 +86,7 @@ describe Gitlab::QueryLimiting::Transaction do
it 'returns false in a production environment' do it 'returns false in a production environment' do
transaction = described_class.new transaction = described_class.new
expect(Rails.env) stub_rails_env('production')
.to receive(:test?)
.and_return(false)
expect(transaction.raise_error?).to eq(false) expect(transaction.raise_error?).to eq(false)
end end

View File

@ -9,14 +9,14 @@ describe Gitlab::QueryLimiting do
end end
it 'returns true in a development environment' do it 'returns true in a development environment' do
allow(Rails.env).to receive(:development?).and_return(true) stub_rails_env('development')
stub_rails_env('development')
expect(described_class.enable?).to eq(true) expect(described_class.enable?).to eq(true)
end end
it 'returns false on GitLab.com' do it 'returns false on GitLab.com' do
expect(Rails.env).to receive(:development?).and_return(false) stub_rails_env('production')
expect(Rails.env).to receive(:test?).and_return(false)
allow(Gitlab).to receive(:com?).and_return(true) allow(Gitlab).to receive(:com?).and_return(true)
expect(described_class.enable?).to eq(false) expect(described_class.enable?).to eq(false)
@ -24,8 +24,7 @@ describe Gitlab::QueryLimiting do
it 'returns false in a non GitLab.com' do it 'returns false in a non GitLab.com' do
allow(Gitlab).to receive(:com?).and_return(false) allow(Gitlab).to receive(:com?).and_return(false)
expect(Rails.env).to receive(:development?).and_return(false) stub_rails_env('production')
expect(Rails.env).to receive(:test?).and_return(false)
expect(described_class.enable?).to eq(false) expect(described_class.enable?).to eq(false)
end end

View File

@ -3,8 +3,6 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab do describe Gitlab do
include RailsHelpers
describe '.root' do describe '.root' do
it 'returns the root path of the app' do it 'returns the root path of the app' do
expect(described_class.root).to eq(Pathname.new(File.expand_path('../..', __dir__))) expect(described_class.root).to eq(Pathname.new(File.expand_path('../..', __dir__)))

View File

@ -131,7 +131,7 @@ describe CacheableAttributes do
context 'in production environment' do context 'in production environment' do
before do before do
expect(Rails.env).to receive(:production?).and_return(true) stub_rails_env('production')
end end
it 'returns an uncached record and logs a warning' do it 'returns an uncached record and logs a warning' do
@ -143,7 +143,7 @@ describe CacheableAttributes do
context 'in other environments' do context 'in other environments' do
before do before do
expect(Rails.env).to receive(:production?).and_return(false) stub_rails_env('development')
end end
it 'returns an uncached record and logs a warning' do it 'returns an uncached record and logs a warning' do

View File

@ -17,7 +17,7 @@ describe ShaAttribute do
describe '#sha_attribute' do describe '#sha_attribute' do
context 'when in non-production' do context 'when in non-production' do
before do before do
allow(Rails.env).to receive(:production?).and_return(false) stub_rails_env('development')
end end
context 'when the table exists' do context 'when the table exists' do
@ -76,7 +76,7 @@ describe ShaAttribute do
context 'when in production' do context 'when in production' do
before do before do
allow(Rails.env).to receive(:production?).and_return(true) stub_rails_env('production')
end end
it 'defines a SHA attribute' do it 'defines a SHA attribute' do

View File

@ -106,7 +106,8 @@ describe InternalId do
end end
it 'always attempts to generate internal IDs in production mode' do it 'always attempts to generate internal IDs in production mode' do
allow(Rails.env).to receive(:test?).and_return(false) stub_rails_env('production')
val = rand(1..100) val = rand(1..100)
generator = double(generate: val) generator = double(generate: val)
expect(InternalId::InternalIdGenerator).to receive(:new).and_return(generator) expect(InternalId::InternalIdGenerator).to receive(:new).and_return(generator)

View File

@ -109,6 +109,7 @@ RSpec.configure do |config|
config.include PolicyHelpers, type: :policy config.include PolicyHelpers, type: :policy
config.include MemoryUsageHelper config.include MemoryUsageHelper
config.include ExpectRequestWithStatus, type: :request config.include ExpectRequestWithStatus, type: :request
config.include RailsHelpers
if ENV['CI'] if ENV['CI']
# This includes the first try, i.e. tests will be run 4 times before failing. # This includes the first try, i.e. tests will be run 4 times before failing.

View File

@ -57,7 +57,7 @@ describe 'gitlab:gitaly namespace rake task' do
stub_env('CI', false) stub_env('CI', false)
FileUtils.mkdir_p(clone_path) FileUtils.mkdir_p(clone_path)
expect(Dir).to receive(:chdir).with(clone_path).and_call_original expect(Dir).to receive(:chdir).with(clone_path).and_call_original
allow(Rails.env).to receive(:test?).and_return(false) stub_rails_env('development')
end end
context 'gmake is available' do context 'gmake is available' do
@ -93,7 +93,7 @@ describe 'gitlab:gitaly namespace rake task' do
end end
before do before do
allow(Rails.env).to receive(:test?).and_return(true) stub_rails_env('test')
end end
it 'calls make in the gitaly directory with --no-deployment flag for bundle' do it 'calls make in the gitaly directory with --no-deployment flag for bundle' do