Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara can't find database records during feature specs

Tags:

I have a JS feature spec I'm trying to run with Capybara Webkit. It doesn't seem to be able to find my database records however.

The spec in question looks like this

it "should allow pledging to a Hardback level", js: true do   book = FactoryGirl.create :book   visit book_path(book)   click_link "pledge-btn" end 

Unfortunately, the request to book_path(book) 404s because the book cannot be found.

If I take the :js flag off, the test passes.

I have DatabaseCleaner set up to use :truncation for JS specs as is the recommended method.

# spec/support/database_cleaner.rb RSpec.configure do |config|   config.use_transactional_fixtures = false    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     DatabaseMetadata.create!(:sanitized_at => DateTime.now)    end    config.after(:each) do     DatabaseCleaner.clean   end end 

I can puts the book in the test and it will be found.

it "should allow pledging to a Hardback level", js: true do   book = FactoryGirl.create :book   visit book_path(book)   p Book.first   # => .. the book instance   click_link "pledge-btn" end 

I've also tried this shared connection method which doesn't seem to fix the problem either.

What else could be wrong?

like image 344
David Tuite Avatar asked Nov 07 '13 12:11

David Tuite


2 Answers

You may have config.use_transactional_fixtures = true set in your spec_helper.rb. This would override what you have above.

You want to either remove this line from your spec_helper.rb or change it there to be false.

like image 195
RubeOnRails Avatar answered Oct 17 '22 07:10

RubeOnRails


I ran into this same issue and had a very similar config. After looking through the DatabaseCleaner README, I found this small note:

It's also recommended to use append_after to ensure DatabaseCleaner.clean runs after the after-test cleanup capybara/rspec installs.

Source

That means changing your

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

to

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

Note the append_after instead of after. This fixed my problem.

like image 29
Nick Avatar answered Oct 17 '22 06:10

Nick