title: Rspec - How to clean the database after each test
body: <p>I have a feature spec with Capybara for a login page, and I am using FactoryGirl + DatabaseCleaner</p>
  
  <pre><code>require 'rails_helper'
  
  feature 'Admin signs in' do
  
    background do
      FactoryGirl.create(:user)
    end
  
    scenario 'with valid credentials' do
      visit admin_root_path
      fill_in 'user_email', :with =&gt; 'email@email.com'
      fill_in 'user_password', :with =&gt; 'testpassword'
      click_button 'Sign in'
      expect(page).to have_content('Dashboard')
    end
  
    scenario 'with invalid credentials' do
      visit admin_root_path
      fill_in 'user_email', :with =&gt; 'email@email.com'
      fill_in 'user_password', :with =&gt; 'wrongpassword'
      click_button 'Sign in'
      expect(page).to have_content('Admin Login')
    end
  
  end
  </code></pre>
  
  <p>running the test, I get the following error:</p>
  
  <pre><code>1) Admin signs in test with invalid credentials
   Failure/Error: FactoryGirl.create(:user)
   ActiveRecord::RecordInvalid:
     Validation failed: Email has already been taken
  </code></pre>
  
  <p>I thought DatabaseCleaner would revert the changes, but it looks like the user record persist in the database till the second scenario block.</p>
  
  <p>How can I make sure that the database is cleaned after the first scenario?</p>
  
  <p>I configured Database cleaner following this <a href="http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/" rel="noreferrer">post</a></p>
  
  <pre><code># support/database_cleaner_spec.rb
  
  RSpec.configure do |config|
    config.before(:suite) do
      DatabaseCleaner.clean_with(:truncation)
    end
  
    config.before(:each) do
      DatabaseCleaner.strategy = :transaction
    end
  
    config.before(:each, :js =&gt; true) do
      DatabaseCleaner.strategy = :truncation
    end
  
    config.before(:each) do
      DatabaseCleaner.start
    end
  
    config.after(:each) do
      DatabaseCleaner.clean
    end
  
  end
  </code></pre>
  
  <p>I have also updated the spec helper file with:</p>
  
  <pre><code>config.use_transactional_fixtures = false
  </code></pre>