Disable database_cleaner for tests that use before(:all)?

1,889 views
Skip to first unread message

mcbsys

unread,
Sep 14, 2012, 9:45:55 PM9/14/12
to database...@googlegroups.com
Hi,

Environment:  Rails 3.2.8, Rspec 2.11.0, Capybara 1.1.2, database_cleaner 0.8.0, Postgres 8.4.

I've recently added some JavaScript to my code. In order to allow Selenium/webkit to see the test data, I have set

  config.use_transactional_fixtures = false

and have set up database_cleaner as follows:

    config.before(:suite) do
      DatabaseCleaner.strategy = :deletion
    end
    config.before(:each) do
      DatabaseCleaner.start
    end
    config.after(:each) do
      DatabaseCleaner.clean
    end

That works fine as long as the test is set up to refresh the data for each test.

However I have some groups of tests where I set up a larger amount of data in a before(:all) block. With transactional fixtures, RSpec knows not to rollback after each test if a before(:all) is used (explained here). And I had to learn to manually add an after(:all) block to delete the data created in before(:all).

Problem is, database_cleaner is wiping the data after each test even if there is a before(:all) block. Is there a way to automatically disable that when before(:all) is present? Or maybe a way to manually disable database_cleaner for one "describe" block and its descendants?

Thanks,

Mark

mcbsys

unread,
Sep 15, 2012, 5:11:13 PM9/15/12
to database...@googlegroups.com
After reading this Rspec issue, I realized I can handle this as follows:

spec_helper.rb

    config.use_transactional_fixtures = true
    config.before(:suite) do
      DatabaseCleaner.strategy = :deletion # :truncation should work, :transaction doesn't work
    end

Within a test block that needs transactions turned off to handle JavaScript:

    before(:all) do # must be before :all so transactions are disabled for entire block
      self.use_transactional_fixtures = false
      DatabaseCleaner.start
    end
    after(:each) do # still deletes records after each test
      DatabaseCleaner.clean
      self.use_transactional_fixtures = true
    end

And if the test block needs to load data once for the whole block:

    before(:all) do
      self.use_transactional_fixtures = false
      DatabaseCleaner.start
      # Create data here (FactoryGirl.create...)
    end
    after(:all) do # Use after :all so data stays around until the end of the block
      DatabaseCleaner.clean
      self.use_transactional_fixtures = true
    end

Seems a little tricky (fragile?) to get all these bits working together...

Mark
Reply all
Reply to author
Forward
0 new messages