From 6957554ad53df15609db66a9e1aaf8dd86ff8059 Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Mon, 2 Apr 2018 16:36:51 -0400 Subject: [PATCH] Tweaked code to use stub_const instead --- spec/database_cleaner/safeguard_spec.rb | 19 ++++++++++--------- spec/support/env.rb | 22 ---------------------- 2 files changed, 10 insertions(+), 31 deletions(-) delete mode 100644 spec/support/env.rb diff --git a/spec/database_cleaner/safeguard_spec.rb b/spec/database_cleaner/safeguard_spec.rb index 34c9841..87bc1a3 100644 --- a/spec/database_cleaner/safeguard_spec.rb +++ b/spec/database_cleaner/safeguard_spec.rb @@ -1,20 +1,19 @@ require 'spec_helper' -require 'support/env' require 'active_record' require 'database_cleaner/active_record/transaction' module DatabaseCleaner describe Safeguard do - include Support::Env - let(:strategy) { DatabaseCleaner::ActiveRecord::Transaction } let(:cleaner) { Base.new(:autodetect) } before { allow_any_instance_of(strategy).to receive(:start) } describe 'DATABASE_URL is set' do + before { stub_const('ENV', 'DATABASE_URL' => database_url) } + describe 'to any value' do - env DATABASE_URL: 'postgres://remote.host' + let(:database_url) { 'postgres://remote.host' } it 'raises' do expect { cleaner.start }.to raise_error(Safeguard::Error::RemoteDatabaseUrl) @@ -22,7 +21,7 @@ module DatabaseCleaner end describe 'to a localhost url' do - env DATABASE_URL: 'postgres://localhost' + let(:database_url) { 'postgres://localhost' } it 'does not raise' do expect { cleaner.start }.to_not raise_error @@ -30,7 +29,7 @@ module DatabaseCleaner end describe 'to a 127.0.0.1 url' do - env DATABASE_URL: 'postgres://127.0.0.1' + let(:database_url) { 'postgres://127.0.0.1' } it 'does not raise' do expect { cleaner.start }.to_not raise_error @@ -38,7 +37,8 @@ module DatabaseCleaner end describe 'DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL is set' do - env DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL: true + let(:database_url) { 'postgres://remote.host' } + before { stub_const('ENV', 'DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL' => true) } it 'does not raise' do expect { cleaner.start }.to_not raise_error @@ -46,6 +46,7 @@ module DatabaseCleaner end describe 'DatabaseCleaner.allow_remote_database_url is true' do + let(:database_url) { 'postgres://remote.host' } before { DatabaseCleaner.allow_remote_database_url = true } after { DatabaseCleaner.allow_remote_database_url = nil } @@ -58,7 +59,7 @@ module DatabaseCleaner describe 'ENV is set to production' do %w(ENV RACK_ENV RAILS_ENV).each do |key| describe "on #{key}" do - env key => 'production' + before { stub_const('ENV', key => "production") } it 'raises' do expect { cleaner.start }.to raise_error(Safeguard::Error::ProductionEnv) @@ -66,7 +67,7 @@ module DatabaseCleaner end describe 'DATABASE_CLEANER_ALLOW_PRODUCTION is set' do - env DATABASE_CLEANER_ALLOW_PRODUCTION: true + before { stub_const('ENV', 'DATABASE_CLEANER_ALLOW_PRODUCTION' => true) } it 'does not raise' do expect { cleaner.start }.to_not raise_error diff --git a/spec/support/env.rb b/spec/support/env.rb deleted file mode 100644 index d3ceca3..0000000 --- a/spec/support/env.rb +++ /dev/null @@ -1,22 +0,0 @@ -module Support - module Env - def self.included(base) - base.send(:extend, ClassMethods) - end - - module ClassMethods - def env(vars) - before { define_env(vars) } - after { undefine_env(vars) } - end - end - - def define_env(vars) - vars.each { |key, value| ENV[key.to_s.upcase] = value.to_s } - end - - def undefine_env(vars) - vars.each { |key, _| ENV.delete(key.to_s) } - end - end -end