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

View File

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

View File

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

View File

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