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
context "db specified" do
subject(:active_record_cleaner_with_specified_connection) do
Base.new(:active_record, connection: :my_db)
end
subject(:cleaner) { Base.new(:active_record, connection: :my_db) }
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
describe "orm" do
it "should store orm" do
cleaner = Base.new :a_orm
cleaner = Base.new(:a_orm)
expect(cleaner.orm).to eq :a_orm
end
it "converts string to symbols" do
cleaner = Base.new "mongoid"
cleaner = Base.new("mongoid")
expect(cleaner.orm).to eq :mongoid
end
it "should default to nil" do
expect(subject.orm).to be_nil
cleaner = Base.new
expect(cleaner.orm).to be_nil
end
end
end
describe "db" do
subject(:cleaner) { Base.new }
it "should default to :default" do
expect(subject.db).to eq :default
expect(cleaner.db).to eq :default
end
it "should return any stored db value" do
subject.db = :test_db
expect(subject.db).to eq :test_db
cleaner.db = :test_db
expect(cleaner.db).to eq :test_db
end
end
describe "db=" do
subject(:cleaner) { Base.new }
context "when strategy supports db specification" do
it "should pass db down to its current strategy" do
expect(subject.strategy).to receive(:db=).with(:a_new_db)
subject.db = :a_new_db
expect(cleaner.strategy).to receive(:db=).with(:a_new_db)
cleaner.db = :a_new_db
end
end
context "when strategy doesn't support db specification" do
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
expect(strategy).to_not receive(:db=)
subject.db = :default
cleaner.db = :default
end
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
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) { double }
@ -103,22 +106,22 @@ module DatabaseCleaner
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).to receive(:clean)
subject.clean_with :truncation, :dollar, :amet, ipsum: "random"
cleaner.clean_with :truncation, :dollar, :amet, ipsum: "random"
end
it "should invoke clean on the created strategy" do
expect(strategy).to receive(:clean)
subject.clean_with :truncation
cleaner.clean_with :truncation
end
it "should return the created strategy" do
expect(strategy).to receive(:clean)
expect(subject.clean_with(:truncation)).to eq strategy
expect(cleaner.clean_with(:truncation)).to eq strategy
end
end
describe "strategy=" do
subject(:active_record_cleaner) { Base.new(:active_record) }
subject(:cleaner) { Base.new(:active_record) }
let(:strategy_class) { Class.new }
@ -133,44 +136,44 @@ module DatabaseCleaner
end
it "should look up and create a the named strategy for the current ORM" do
subject.strategy = :truncation
expect(subject.strategy).to be_a(strategy_class)
cleaner.strategy = :truncation
expect(cleaner.strategy).to be_a(strategy_class)
end
it "should proxy params with symbolised strategies" do
expect(strategy_class).to receive(:new).with(param: "one")
subject.strategy = :truncation, { param: "one" }
cleaner.strategy = :truncation, { param: "one" }
end
it "should accept strategy objects" do
strategy = double
subject.strategy = strategy
expect(subject.strategy).to eq strategy
cleaner.strategy = strategy
expect(cleaner.strategy).to eq strategy
end
it "should raise argument error when params given with strategy object" do
expect do
subject.strategy = double, { param: "one" }
cleaner.strategy = double, { param: "one" }
end.to raise_error ArgumentError
end
it "should attempt to set strategy db" do
strategy = double
expect(strategy).to receive(:db=).with(:default)
subject.strategy = strategy
cleaner.strategy = strategy
end
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")
end
end
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
expect(subject.strategy).to be_a(DatabaseCleaner::NullStrategy)
expect(cleaner.strategy).to be_a(DatabaseCleaner::NullStrategy)
end
end
@ -178,34 +181,37 @@ module DatabaseCleaner
let(:mock_orm) { double("orm") }
it "should return orm if orm set" do
subject.orm = :desired_orm
expect(subject.orm).to eq :desired_orm
cleaner = Base.new
cleaner.orm = :desired_orm
expect(cleaner.orm).to eq :desired_orm
end
end
describe "proxy methods" do
subject(:cleaner) { Base.new }
let(:strategy) { double(:strategy) }
before { subject.strategy = strategy }
before { cleaner.strategy = strategy }
describe "start" do
it "should proxy start to the strategy" do
expect(strategy).to receive(:start)
subject.start
cleaner.start
end
end
describe "clean" do
it "should proxy clean to the strategy" do
expect(strategy).to receive(:clean)
subject.clean
cleaner.clean
end
end
describe "cleaning" do
it "should proxy cleaning to the strategy" do
expect(strategy).to receive(:cleaning)
subject.cleaning { }
cleaner.cleaning { }
end
end
end

View file

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

View file

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