I can't figure out why my example fails sometimes and sometimes not:

describe ClientsController do
  let(:admin) { create(:admin) }

  before(:each) { sign_in admin }

  describe 'GET #index' do
    Client.delete_all
    let!(:clients) { Array.new(3) { create(:client) } }
    it "populates an array of all clients" do

      get :index
      expect(assigns(:clients)).to match_array(clients)
    end

    it "renders the :index template" do
      get :index
      expect(response).to render_template :index
    end
   end
end

I set up DatabaseCleaner as follows in spec_helper:

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

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end

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

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

and set up fixtures to false as well (spec_helper):

config.use_transactional_fixtures = false

No matter if I keep the line or not:

Client.delete_all

it fails the first time (when I run all the tests) and passes when I run 
the spec separately.

I also defined a shared_db_connection in support folder:

class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection

For a more complete description of this setup, check out Avdi Grimm’s 
Virtuous Code blog:
  
http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

Any idea? THANK YOU

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/182ba14a-0cfe-441a-84d2-5a35d585a2b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to