Skip to content

Instantly share code, notes, and snippets.

@weilu
Created November 10, 2012 08:12
Show Gist options
  • Save weilu/4050393 to your computer and use it in GitHub Desktop.
Save weilu/4050393 to your computer and use it in GitHub Desktop.
Configure database_cleaner for a test suite that contains Capybara js specs.
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with :truncation
end
config.before(:each) do
if example.metadata[:js]
DatabaseCleaner.strategy = :truncation
else
DatabaseCleaner.strategy = :transaction
end
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
@tgaff
Copy link

tgaff commented May 14, 2015

Note: example.metadata is no longer available in the config.before(:each) block from at least 3.2.0. See: https://github.com/rspec/rspec-core/blob/13c6674f8021ab7f00dc70559871169d2595cc12/Changelog.md The error message describes this best:

*** RSpec::Core::ExampleGroup::WrongScopeError Exception: `metadata` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).

One can get around this by using config.before(:each) do |example|

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment