reduce usage of bare subject on advice of rspec core team.

This commit is contained in:
Micah Geisel 2020-02-04 07:45:54 -08:00 committed by Micah Geisel
parent 73afd350d5
commit a5928f07b4
3 changed files with 99 additions and 89 deletions

View file

@ -28,69 +28,72 @@ module DatabaseCleaner
describe "initialization" do describe "initialization" do
context "db specified" do context "db specified" do
subject(:active_record_cleaner_with_specified_connection) do subject(:cleaner) { Base.new(:active_record, connection: :my_db) }
Base.new(:active_record, connection: :my_db)
end
it "should store db from :connection in params hash" do it "should store db from :connection in params hash" do
expect(subject.db).to eq :my_db expect(cleaner.db).to eq :my_db
end end
end end
describe "orm" do describe "orm" do
it "should store orm" do it "should store orm" do
cleaner = Base.new :a_orm cleaner = Base.new(:a_orm)
expect(cleaner.orm).to eq :a_orm expect(cleaner.orm).to eq :a_orm
end end
it "converts string to symbols" do it "converts string to symbols" do
cleaner = Base.new "mongoid" cleaner = Base.new("mongoid")
expect(cleaner.orm).to eq :mongoid expect(cleaner.orm).to eq :mongoid
end end
it "should default to nil" do it "should default to nil" do
expect(subject.orm).to be_nil cleaner = Base.new
expect(cleaner.orm).to be_nil
end end
end end
end end
describe "db" do describe "db" do
subject(:cleaner) { Base.new }
it "should default to :default" do it "should default to :default" do
expect(subject.db).to eq :default expect(cleaner.db).to eq :default
end end
it "should return any stored db value" do it "should return any stored db value" do
subject.db = :test_db cleaner.db = :test_db
expect(subject.db).to eq :test_db expect(cleaner.db).to eq :test_db
end end
end end
describe "db=" do describe "db=" do
subject(:cleaner) { Base.new }
context "when strategy supports db specification" do context "when strategy supports db specification" do
it "should pass db down to its current strategy" do it "should pass db down to its current strategy" do
expect(subject.strategy).to receive(:db=).with(:a_new_db) expect(cleaner.strategy).to receive(:db=).with(:a_new_db)
subject.db = :a_new_db cleaner.db = :a_new_db
end end
end end
context "when strategy doesn't support db specification" do context "when strategy doesn't support db specification" do
let(:strategy) { double(respond_to?: false) } let(:strategy) { double(respond_to?: false) }
before { subject.strategy = strategy } before { cleaner.strategy = strategy }
it "doesn't pass the default db down to it" do it "doesn't pass the default db down to it" do
expect(strategy).to_not receive(:db=) expect(strategy).to_not receive(:db=)
subject.db = :default cleaner.db = :default
end end
it "should raise an argument error when db isn't default" do it "should raise an argument error when db isn't default" do
expect { subject.db = :test }.to raise_error ArgumentError expect { cleaner.db = :test }.to raise_error ArgumentError
end end
end end
end end
describe "clean_with" do describe "clean_with" do
subject(:active_record_cleaner) { Base.new(:active_record) } subject(:cleaner) { Base.new(:active_record) }
let(:strategy_class) { Class.new } let(:strategy_class) { Class.new }
let(:strategy) { double } let(:strategy) { double }
@ -103,22 +106,22 @@ module DatabaseCleaner
it "should pass all arguments to strategy initializer" do it "should pass all arguments to strategy initializer" do
expect(strategy_class).to receive(:new).with(:dollar, :amet, ipsum: "random").and_return(strategy) expect(strategy_class).to receive(:new).with(:dollar, :amet, ipsum: "random").and_return(strategy)
expect(strategy).to receive(:clean) expect(strategy).to receive(:clean)
subject.clean_with :truncation, :dollar, :amet, ipsum: "random" cleaner.clean_with :truncation, :dollar, :amet, ipsum: "random"
end end
it "should invoke clean on the created strategy" do it "should invoke clean on the created strategy" do
expect(strategy).to receive(:clean) expect(strategy).to receive(:clean)
subject.clean_with :truncation cleaner.clean_with :truncation
end end
it "should return the created strategy" do it "should return the created strategy" do
expect(strategy).to receive(:clean) expect(strategy).to receive(:clean)
expect(subject.clean_with(:truncation)).to eq strategy expect(cleaner.clean_with(:truncation)).to eq strategy
end end
end end
describe "strategy=" do describe "strategy=" do
subject(:active_record_cleaner) { Base.new(:active_record) } subject(:cleaner) { Base.new(:active_record) }
let(:strategy_class) { Class.new } let(:strategy_class) { Class.new }
@ -133,44 +136,44 @@ module DatabaseCleaner
end end
it "should look up and create a the named strategy for the current ORM" do it "should look up and create a the named strategy for the current ORM" do
subject.strategy = :truncation cleaner.strategy = :truncation
expect(subject.strategy).to be_a(strategy_class) expect(cleaner.strategy).to be_a(strategy_class)
end end
it "should proxy params with symbolised strategies" do it "should proxy params with symbolised strategies" do
expect(strategy_class).to receive(:new).with(param: "one") expect(strategy_class).to receive(:new).with(param: "one")
subject.strategy = :truncation, { param: "one" } cleaner.strategy = :truncation, { param: "one" }
end end
it "should accept strategy objects" do it "should accept strategy objects" do
strategy = double strategy = double
subject.strategy = strategy cleaner.strategy = strategy
expect(subject.strategy).to eq strategy expect(cleaner.strategy).to eq strategy
end end
it "should raise argument error when params given with strategy object" do it "should raise argument error when params given with strategy object" do
expect do expect do
subject.strategy = double, { param: "one" } cleaner.strategy = double, { param: "one" }
end.to raise_error ArgumentError end.to raise_error ArgumentError
end end
it "should attempt to set strategy db" do it "should attempt to set strategy db" do
strategy = double strategy = double
expect(strategy).to receive(:db=).with(:default) expect(strategy).to receive(:db=).with(:default)
subject.strategy = strategy cleaner.strategy = strategy
end end
it "raises UnknownStrategySpecified on a bad strategy, and lists available strategies" do it "raises UnknownStrategySpecified on a bad strategy, and lists available strategies" do
expect { subject.strategy = :horrible_plan }.to \ expect { cleaner.strategy = :horrible_plan }.to \
raise_error(UnknownStrategySpecified, "The 'horrible_plan' strategy does not exist for the active_record ORM! Available strategies: truncation, transaction, deletion") raise_error(UnknownStrategySpecified, "The 'horrible_plan' strategy does not exist for the active_record ORM! Available strategies: truncation, transaction, deletion")
end end
end end
describe "strategy" do describe "strategy" do
subject(:invalid_cleaner) { Base.new(:a_orm) } subject(:cleaner) { Base.new(:a_orm) }
it "returns a null strategy when strategy is not set and undetectable" do it "returns a null strategy when strategy is not set and undetectable" do
expect(subject.strategy).to be_a(DatabaseCleaner::NullStrategy) expect(cleaner.strategy).to be_a(DatabaseCleaner::NullStrategy)
end end
end end
@ -178,34 +181,37 @@ module DatabaseCleaner
let(:mock_orm) { double("orm") } let(:mock_orm) { double("orm") }
it "should return orm if orm set" do it "should return orm if orm set" do
subject.orm = :desired_orm cleaner = Base.new
expect(subject.orm).to eq :desired_orm cleaner.orm = :desired_orm
expect(cleaner.orm).to eq :desired_orm
end end
end end
describe "proxy methods" do describe "proxy methods" do
subject(:cleaner) { Base.new }
let(:strategy) { double(:strategy) } let(:strategy) { double(:strategy) }
before { subject.strategy = strategy } before { cleaner.strategy = strategy }
describe "start" do describe "start" do
it "should proxy start to the strategy" do it "should proxy start to the strategy" do
expect(strategy).to receive(:start) expect(strategy).to receive(:start)
subject.start cleaner.start
end end
end end
describe "clean" do describe "clean" do
it "should proxy clean to the strategy" do it "should proxy clean to the strategy" do
expect(strategy).to receive(:clean) expect(strategy).to receive(:clean)
subject.clean cleaner.clean
end end
end end
describe "cleaning" do describe "cleaning" do
it "should proxy cleaning to the strategy" do it "should proxy cleaning to the strategy" do
expect(strategy).to receive(:cleaning) expect(strategy).to receive(:cleaning)
subject.cleaning { } cleaner.cleaning { }
end end
end end
end end

View file

@ -9,127 +9,129 @@ module DatabaseCleaner
end end
RSpec.describe DatabaseCleaner::Configuration do RSpec.describe DatabaseCleaner::Configuration do
subject(:config) { described_class.new }
context "orm specification" do context "orm specification" do
it "should not accept unrecognised orms" do it "should not accept unrecognised orms" do
expect { subject[nil] }.to raise_error(DatabaseCleaner::NoORMDetected) expect { config[nil] }.to raise_error(DatabaseCleaner::NoORMDetected)
end end
it "should accept :active_record" do it "should accept :active_record" do
cleaner = subject[:active_record] cleaner = config[:active_record]
expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :active_record expect(cleaner.orm).to eq :active_record
expect(subject.cleaners.values).to eq [cleaner] expect(config.cleaners.values).to eq [cleaner]
end end
it "should accept :data_mapper" do it "should accept :data_mapper" do
cleaner = subject[:data_mapper] cleaner = config[:data_mapper]
expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :data_mapper expect(cleaner.orm).to eq :data_mapper
expect(subject.cleaners.values).to eq [cleaner] expect(config.cleaners.values).to eq [cleaner]
end end
it "should accept :mongo_mapper" do it "should accept :mongo_mapper" do
cleaner = subject[:mongo_mapper] cleaner = config[:mongo_mapper]
expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :mongo_mapper expect(cleaner.orm).to eq :mongo_mapper
expect(subject.cleaners.values).to eq [cleaner] expect(config.cleaners.values).to eq [cleaner]
end end
it "should accept :couch_potato" do it "should accept :couch_potato" do
cleaner = subject[:couch_potato] cleaner = config[:couch_potato]
expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :couch_potato expect(cleaner.orm).to eq :couch_potato
expect(subject.cleaners.values).to eq [cleaner] expect(config.cleaners.values).to eq [cleaner]
end end
it "should accept :moped" do it "should accept :moped" do
cleaner = subject[:moped] cleaner = config[:moped]
expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :moped expect(cleaner.orm).to eq :moped
expect(subject.cleaners.values).to eq [cleaner] expect(config.cleaners.values).to eq [cleaner]
end end
it 'accepts :ohm' do it 'accepts :ohm' do
cleaner = subject[:ohm] cleaner = config[:ohm]
expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :ohm expect(cleaner.orm).to eq :ohm
expect(subject.cleaners.values).to eq [cleaner] expect(config.cleaners.values).to eq [cleaner]
end end
it "should accept multiple orm's" do it "should accept multiple orm's" do
cleaners = [subject[:couch_potato], subject[:data_mapper]] cleaners = [config[:couch_potato], config[:data_mapper]]
expect(subject.cleaners.values.map(&:orm)).to eq [:couch_potato, :data_mapper] expect(config.cleaners.values.map(&:orm)).to eq [:couch_potato, :data_mapper]
expect(subject.cleaners.values).to eq cleaners expect(config.cleaners.values).to eq cleaners
end end
it "should accept a connection parameter and store it" do it "should accept a connection parameter and store it" do
cleaner = subject[:active_record, connection: :first_connection] cleaner = config[:active_record, connection: :first_connection]
expect(cleaner).to be_a(DatabaseCleaner::Base) expect(cleaner).to be_a(DatabaseCleaner::Base)
expect(cleaner.orm).to eq :active_record expect(cleaner.orm).to eq :active_record
expect(cleaner.db).to eq :first_connection expect(cleaner.db).to eq :first_connection
end end
it "should accept multiple connections for a single orm" do it "should accept multiple connections for a single orm" do
subject[:data_mapper, connection: :first_db] config[:data_mapper, connection: :first_db]
subject[:data_mapper, connection: :second_db] config[:data_mapper, connection: :second_db]
expect(subject.cleaners.values.map(&:orm)).to eq [:data_mapper, :data_mapper] expect(config.cleaners.values.map(&:orm)).to eq [:data_mapper, :data_mapper]
expect(subject.cleaners.values.map(&:db)).to eq [:first_db, :second_db] expect(config.cleaners.values.map(&:db)).to eq [:first_db, :second_db]
end end
it "should accept multiple connections and multiple orms" do it "should accept multiple connections and multiple orms" do
subject[:data_mapper, connection: :first_db ] config[:data_mapper, connection: :first_db ]
subject[:active_record, connection: :second_db] config[:active_record, connection: :second_db]
subject[:active_record, connection: :first_db ] config[:active_record, connection: :first_db ]
subject[:data_mapper, connection: :second_db] config[:data_mapper, connection: :second_db]
expect(subject.cleaners.values.map(&:orm)).to eq [:data_mapper, :active_record, :active_record, :data_mapper] expect(config.cleaners.values.map(&:orm)).to eq [:data_mapper, :active_record, :active_record, :data_mapper]
expect(subject.cleaners.values.map(&:db)).to eq [:first_db, :second_db, :first_db, :second_db] expect(config.cleaners.values.map(&:db)).to eq [:first_db, :second_db, :first_db, :second_db]
end end
it "should retrieve a db rather than create a new one" do it "should retrieve a db rather than create a new one" do
stub_const "DatabaseCleaner::ActiveRecord", Module.new stub_const "DatabaseCleaner::ActiveRecord", Module.new
stub_const "DatabaseCleaner::ActiveRecord::Truncation", Class.new stub_const "DatabaseCleaner::ActiveRecord::Truncation", Class.new
connection = subject[:active_record] connection = config[:active_record]
subject[:active_record].strategy = :truncation config[:active_record].strategy = :truncation
expect(subject[:active_record]).to equal connection expect(config[:active_record]).to equal connection
end end
end end
context "top level api methods" do context "top level api methods" do
context "single orm single connection" do context "single orm single connection" do
let(:connection) { subject[:active_record] } let(:connection) { config[:active_record] }
it "should proxy strategy=" do it "should proxy strategy=" do
stratagem = double("stratagem") stratagem = double("stratagem")
expect(connection).to receive(:strategy=).with(stratagem) expect(connection).to receive(:strategy=).with(stratagem)
subject.strategy = stratagem config.strategy = stratagem
end end
it "should proxy orm=" do it "should proxy orm=" do
orm = double("orm") orm = double("orm")
expect(connection).to receive(:orm=).with(orm) expect(connection).to receive(:orm=).with(orm)
subject.orm = orm config.orm = orm
end end
it "should proxy start" do it "should proxy start" do
expect(connection).to receive(:start) expect(connection).to receive(:start)
subject.start config.start
end end
it "should proxy clean" do it "should proxy clean" do
expect(connection).to receive(:clean) expect(connection).to receive(:clean)
subject.clean config.clean
end end
it 'should proxy cleaning' do it 'should proxy cleaning' do
expect(connection).to receive(:cleaning) expect(connection).to receive(:cleaning)
subject.cleaning { } config.cleaning { }
end end
it "should proxy clean_with" do it "should proxy clean_with" do
stratagem = double("stratgem") stratagem = double("stratgem")
expect(connection).to receive(:clean_with).with(stratagem, {}) expect(connection).to receive(:clean_with).with(stratagem, {})
subject.clean_with stratagem, {} config.clean_with stratagem, {}
end end
end end
@ -141,28 +143,28 @@ RSpec.describe DatabaseCleaner::Configuration do
let(:data_mapper) { double("data_mock") } let(:data_mapper) { double("data_mock") }
before do before do
subject.stub_cleaners([active_record,data_mapper]) config.stub_cleaners([active_record,data_mapper])
end end
it "should proxy orm to all cleaners" do it "should proxy orm to all cleaners" do
expect(active_record).to receive(:orm=) expect(active_record).to receive(:orm=)
expect(data_mapper).to receive(:orm=) expect(data_mapper).to receive(:orm=)
subject.orm = double("orm") config.orm = double("orm")
end end
it "should proxy start to all cleaners" do it "should proxy start to all cleaners" do
expect(active_record).to receive(:start) expect(active_record).to receive(:start)
expect(data_mapper).to receive(:start) expect(data_mapper).to receive(:start)
subject.start config.start
end end
it "should proxy clean to all cleaners" do it "should proxy clean to all cleaners" do
expect(active_record).to receive(:clean) expect(active_record).to receive(:clean)
expect(data_mapper).to receive(:clean) expect(data_mapper).to receive(:clean)
subject.clean config.clean
end end
it "should proxy clean_with to all cleaners" do it "should proxy clean_with to all cleaners" do
@ -170,7 +172,7 @@ RSpec.describe DatabaseCleaner::Configuration do
expect(active_record).to receive(:clean_with).with(stratagem) expect(active_record).to receive(:clean_with).with(stratagem)
expect(data_mapper).to receive(:clean_with).with(stratagem) expect(data_mapper).to receive(:clean_with).with(stratagem)
subject.clean_with stratagem config.clean_with stratagem
end end
it "should initiate cleaning on each connection, yield, and finish cleaning each connection" do it "should initiate cleaning on each connection, yield, and finish cleaning each connection" do
@ -187,7 +189,7 @@ RSpec.describe DatabaseCleaner::Configuration do
end end
yielded = false yielded = false
subject.cleaning do config.cleaning do
expect(active_record.started).to eq(true) expect(active_record.started).to eq(true)
expect(data_mapper.started).to eq(true) expect(data_mapper.started).to eq(true)
expect(active_record.cleaned).to eq(nil) expect(active_record.cleaned).to eq(nil)
@ -212,12 +214,12 @@ RSpec.describe DatabaseCleaner::Configuration do
let(:data_mapper_1) { FakeStrategy.new(:data_mapper) } let(:data_mapper_1) { FakeStrategy.new(:data_mapper) }
before do before do
subject.stub_cleaners [active_record_1,active_record_2,data_mapper_1] config.stub_cleaners [active_record_1,active_record_2,data_mapper_1]
end end
it "should proxy #orm= to all cleaners and remove duplicate cleaners" do it "should proxy #orm= to all cleaners and remove duplicate cleaners" do
expect { subject.orm = :data_mapper } expect { config.orm = :data_mapper }
.to change { subject.cleaners.values } .to change { config.cleaners.values }
.from([active_record_1,active_record_2,data_mapper_1]) .from([active_record_1,active_record_2,data_mapper_1])
.to([active_record_1,active_record_2]) .to([active_record_1,active_record_2])
end end
@ -228,12 +230,12 @@ RSpec.describe DatabaseCleaner::Configuration do
let(:active_record_2) { FakeStrategy.new(:active_record, :default, :transaction) } let(:active_record_2) { FakeStrategy.new(:active_record, :default, :transaction) }
before do before do
subject.stub_cleaners [active_record_1,active_record_2] config.stub_cleaners [active_record_1,active_record_2]
end end
it "should proxy #strategy= to all cleaners and remove duplicate cleaners" do it "should proxy #strategy= to all cleaners and remove duplicate cleaners" do
expect { subject.strategy = :truncation } expect { config.strategy = :truncation }
.to change { subject.cleaners.values } .to change { config.cleaners.values }
.from([active_record_1,active_record_2]) .from([active_record_1,active_record_2])
.to([active_record_1]) .to([active_record_1])
end end

View file

@ -2,24 +2,26 @@ require "database_cleaner/null_strategy"
module DatabaseCleaner module DatabaseCleaner
RSpec.describe NullStrategy do RSpec.describe NullStrategy do
subject(:strategy) { NullStrategy.new }
it 'responds to .start' do it 'responds to .start' do
expect { subject.start }.not_to raise_error expect { strategy.start }.not_to raise_error
end end
it 'responds to .clean' do it 'responds to .clean' do
expect { subject.clean }.not_to raise_error expect { strategy.clean }.not_to raise_error
end end
describe '.cleaning' do describe '.cleaning' do
it 'fails without a block' do it 'fails without a block' do
expect { subject.cleaning }.to raise_error(LocalJumpError) expect { strategy.cleaning }.to raise_error(LocalJumpError)
end end
it 'no-ops with a block' do it 'no-ops with a block' do
effect = double effect = double
expect(effect).to receive(:occur).once expect(effect).to receive(:occur).once
subject.cleaning do strategy.cleaning do
effect.occur effect.occur
end end
end end