DatabaseCleaner + RSpec : what is the correct configuration?
Asked Answered
U

3

22

I included database_cleaner gem in my rails app. Followed the example given on the git repo and included the following code in spec_helper :

Approach 1

  config.before(:suite) do
   DatabaseCleaner.strategy = :transaction
   DatabaseCleaner.clean_with(:truncation)
  end

  config.around(:each) do |example|
   DatabaseCleaner.cleaning do
    example.run
   end
  end

When i run the rspec i get error as NoMethodError:undefined method 'cleaning' for DatabaseCleaner:Module.

So i did some research and found that i could replace the config.around block above with something like this :

Approach 2

config.before(:each) do
 DatabaseCleaner.start
end

config.after(:each) do
 DatabaseCleaner.clean
end 

OR

Approach 3

config.around(:each) do |example|
  DatabaseCleaner.start
  example.run
  DatabaseCleaner.clean
end

Both Approach 2 and 3 work well.
I also looked in the git repo of database_cleaner and found that cleaning method actually exists and with the following code :

def cleaning(&block)
     start
     yield
     clean
   end

which is exactly same as what i did in example 3. If it does exists then why is it not accessible? Am i missing something here. Any more setup? Or is Approach 2 or 3 preferable?

Udo answered 1/2, 2014 at 4:9 Comment(0)
U
25

Finally found the answer,

database_cleaner gem added the cleaning method just last week and also updated the documentation for the same. BUT this change is not available in latest gem version 1.2.0 which I sourced from rubygems.org. Approach 1 works perfectly when i source the gem from github as below:

gem 'database_cleaner', git: '[email protected]:DatabaseCleaner/database_cleaner.git'
Udo answered 1/2, 2014 at 4:44 Comment(0)
M
1

You can use the approach in the documentation if you pull the gem from Github

gem 'database_cleaner', git: '[email protected]:bmabey/database_cleaner.git'

Mythical answered 4/2, 2014 at 15:42 Comment(2)
Thats what i did for it to work. Check my answer above.Udo
I know. Just extracted the actual Gemfile entry for lazy people ;)Mythical
P
-1

If you have this same issue using mongoid you can add this to Gemfile, change version to suit you, and run bundle install.

gem 'database_cleaner', '~> 1.4.1'

Then create a support folder in

spec/support/database_cleaner.rb

Require database_cleaner.rb in your spec_helper file, I use gem 'require_all' like so:

# spec/spec_helper.rb
require 'require_all'

require_rel 'support'

Add the following cleaners to database_cleaner.rb

RSpec.configure do |config|

  # Cleanup the DB in between test runs
  config.before(:suite) do
    DatabaseCleaner[:mongoid].strategy = :truncation
    DatabaseCleaner[:mongoid].clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

end

Your tests should now tear down properly.

Pane answered 13/4, 2015 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.