replace ad-hoc ensure_counts with rspec's expect({}).to change({}).

This commit is contained in:
Micah Geisel 2018-04-26 11:15:44 -07:00
parent 4205280099
commit 0fbddcc03b
3 changed files with 27 additions and 59 deletions

View file

@ -19,15 +19,6 @@ module DatabaseCleaner
@connection.drop_database(@test_db)
end
def ensure_counts(expected_counts)
# I had to add this sanity_check garbage because I was getting non-determinisc results from mongo at times..
# very odd and disconcerting...
expected_counts.each do |model_class, expected_count|
actual_count = model_class.count
expect(actual_count).to eq(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{actual_count}"
end
end
def create_widget(attrs={})
MongoTest::Widget.new({:name => 'some widget'}.merge(attrs)).save!
end
@ -39,9 +30,9 @@ module DatabaseCleaner
it "truncates all collections by default" do
create_widget
create_gadget
ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 1)
truncation.clean
ensure_counts(MongoTest::Widget => 0, MongoTest::Gadget => 0)
expect { truncation.clean }.to change {
[MongoTest::Widget.count, MongoTest::Gadget.count]
}.from([1,1]).to([0,0])
end
context "when collections are provided to the :only option" do
@ -49,9 +40,9 @@ module DatabaseCleaner
it "only truncates the specified collections" do
create_widget
create_gadget
ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 1)
truncation.clean
ensure_counts(MongoTest::Widget => 0, MongoTest::Gadget => 1)
expect { truncation.clean }.to change {
[MongoTest::Widget.count, MongoTest::Gadget.count]
}.from([1,1]).to([0,1])
end
end
@ -60,9 +51,9 @@ module DatabaseCleaner
it "truncates all but the specified collections" do
create_widget
create_gadget
ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 1)
truncation.clean
ensure_counts(MongoTest::Widget => 1, MongoTest::Gadget => 0)
expect { truncation.clean }.to change {
[MongoTest::Widget.count, MongoTest::Gadget.count]
}.from([1,1]).to([1,0])
end
end

View file

@ -18,20 +18,6 @@ module DatabaseCleaner
::MongoMapper.connection.drop_database(@test_db)
end
def ensure_counts(expected_counts)
# I had to add this sanity_check garbage because I was getting non-determinisc results from mongomapper at times..
# very odd and disconcerting...
sanity_check = expected_counts.delete(:sanity_check)
begin
expected_counts.each do |model_class, expected_count|
actual_count = model_class.count
expect(actual_count).to eq(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{actual_count}"
end
rescue RSpec::Expectations::ExpectationNotMetError => e
raise !sanity_check ? e : RSpec::ExpectationNotMetError::ExpectationNotMetError.new("SANITY CHECK FAILURE! This should never happen here: #{e.message}")
end
end
def create_widget(attrs={})
Widget.new({:name => 'some widget'}.merge(attrs)).save!
end
@ -43,18 +29,18 @@ module DatabaseCleaner
it "truncates all collections by default" do
create_widget
create_gadget
ensure_counts(Widget => 1, Gadget => 1, :sanity_check => true)
Truncation.new.clean
ensure_counts(Widget => 0, Gadget => 0)
expect { Truncation.new.clean }.to change {
[Widget.count, Gadget.count]
}.from([1,1]).to([0,0])
end
context "when collections are provided to the :only option" do
it "only truncates the specified collections" do
create_widget
create_gadget
ensure_counts(Widget => 1, Gadget => 1, :sanity_check => true)
Truncation.new(:only => ['widgets']).clean
ensure_counts(Widget => 0, Gadget => 1)
expect { Truncation.new(only: ['widgets']).clean }.to change {
[Widget.count, Gadget.count]
}.from([1,1]).to([0,1])
end
end
@ -62,9 +48,9 @@ module DatabaseCleaner
it "truncates all but the specified collections" do
create_widget
create_gadget
ensure_counts(Widget => 1, Gadget => 1, :sanity_check => true)
Truncation.new(:except => ['widgets']).clean
ensure_counts(Widget => 1, Gadget => 0)
expect { Truncation.new(except: ['widgets']).clean }.to change {
[Widget.count, Gadget.count]
}.from([1,1]).to([1,0])
end
end

View file

@ -23,15 +23,6 @@ module DatabaseCleaner
@session.command(getlasterror: 1)
end
def ensure_counts(expected_counts)
# I had to add this sanity_check garbage because I was getting non-deterministic results from mongo at times..
# very odd and disconcerting...
expected_counts.each do |model_class, expected_count|
actual_count = model_class.count
expect(actual_count).to eq(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{actual_count}"
end
end
def create_widget(attrs={})
MopedTest::Widget.new({:name => 'some widget'}.merge(attrs)).save!
end
@ -48,9 +39,9 @@ module DatabaseCleaner
create_widget
create_gadget
create_system
ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 1, MopedTest::System => 1)
truncation.clean
ensure_counts(MopedTest::Widget => 0, MopedTest::Gadget => 0, MopedTest::System => 0)
expect { truncation.clean }.to change {
[MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count]
}.from([1,1,1]).to([0,0,0])
end
context "when collections are provided to the :only option" do
@ -58,9 +49,9 @@ module DatabaseCleaner
it "only truncates the specified collections" do
create_widget
create_gadget
ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 1)
truncation.clean
ensure_counts(MopedTest::Widget => 0, MopedTest::Gadget => 1)
expect { truncation.clean }.to change {
[MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count]
}.from([1,1,0]).to([0,1,0])
end
end
@ -69,9 +60,9 @@ module DatabaseCleaner
it "truncates all but the specified collections" do
create_widget
create_gadget
ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 1)
truncation.clean
ensure_counts(MopedTest::Widget => 1, MopedTest::Gadget => 0)
expect { truncation.clean }.to change {
[MopedTest::Widget.count, MopedTest::Gadget.count, MopedTest::System.count]
}.from([1,1,0]).to([1,0,0])
end
end