raise ArgumentError when DC[nil] is invoked or DC.orm = nil.

This commit is contained in:
Micah Geisel 2020-02-14 12:13:38 -08:00 committed by Micah Geisel
parent b555ef6e87
commit cdaec35574
4 changed files with 12 additions and 13 deletions

View file

@ -11,8 +11,8 @@ module DatabaseCleaner
[orm, db] <=> [other.orm, other.db] [orm, db] <=> [other.orm, other.db]
end end
def initialize(desired_orm = nil, opts = {}) def initialize(orm = :null, opts = {})
self.orm = desired_orm self.orm = orm
self.db = opts[:connection] || opts[:model] if opts.has_key?(:connection) || opts.has_key?(:model) self.db = opts[:connection] || opts[:model] if opts.has_key?(:connection) || opts.has_key?(:model)
Safeguard.new.run Safeguard.new.run
end end
@ -44,8 +44,9 @@ module DatabaseCleaner
attr_reader :orm attr_reader :orm
def orm=(desired_orm) def orm= orm
@orm = desired_orm && desired_orm.to_sym raise ArgumentError if orm.nil?
@orm = orm.to_sym
end end
extend Forwardable extend Forwardable

View file

@ -4,13 +4,12 @@ require 'forwardable'
module DatabaseCleaner module DatabaseCleaner
class NoORMDetected < StandardError; end
class UnknownStrategySpecified < ArgumentError; end class UnknownStrategySpecified < ArgumentError; end
class Cleaners < Hash class Cleaners < Hash
# FIXME this method conflates creation with lookup... both a command and a query. yuck. # FIXME this method conflates creation with lookup... both a command and a query. yuck.
def [](orm, opts = {}) def [](orm, opts = {})
raise NoORMDetected unless orm raise ArgumentError if orm.nil?
fetch([orm, opts]) { add_cleaner(orm, opts) } fetch([orm, opts]) { add_cleaner(orm, opts) }
end end

View file

@ -46,15 +46,14 @@ module DatabaseCleaner
expect(cleaner.orm).to eq :mongoid expect(cleaner.orm).to eq :mongoid
end end
it "should default to nil" do it "should default to :null" do
cleaner = Base.new cleaner = Base.new
expect(cleaner.orm).to be_nil expect(cleaner.orm).to eq :null
end end
it "can handle being set to nil" do it "raises ArgumentError when explicitly set to nil" do
cleaner = Base.new cleaner = Base.new
cleaner.orm = nil expect { cleaner.orm = nil }.to raise_error(ArgumentError)
expect(cleaner.orm).to be_nil
end end
end end
end end

View file

@ -12,8 +12,8 @@ RSpec.describe DatabaseCleaner::Configuration do
subject(:config) { described_class.new } subject(:config) { described_class.new }
context "orm specification" do context "orm specification" do
it "should not accept unrecognised orms" do it "should not accept nil orms" do
expect { config[nil] }.to raise_error(DatabaseCleaner::NoORMDetected) expect { config[nil] }.to raise_error(ArgumentError)
end end
it "should accept :active_record" do it "should accept :active_record" do