Capybara + Poltergeist with DB Cleaner not finding FactoryGirl record
I am writing integration tests and creating records with FactoryGirl
. My controllers are throwing RecordNotFound
when the test has js: true
(using Poltergeist) even though they are in non-js test (no poltergeist).
I have use_transactional_fixtures
for false
and DatabaseCleaner.strategy
set to a value :truncation
that seems to cover every existing SO question on this issue. I tried replacing Selenium (from Firefox) with Poltergeist, but I get the same result. ETA I started a new Rails 4.2.3 project with RSpec-Rails 3.3.3, Capybara 2.4.4, Poltergeist 1.6.0, PhantomJS 1.9.8, Database Cleaner 1.4.1 and got the same results when testing a new, unedited forest.
Why are my notes not found by the Poltergeist? Any suggestions would be helpful as I've been at this for hours.
The first two tests pass until the last one is executed on the second line with RecordNotFound
:
require 'spec_helper'
before :each do
@vehicle = FactoryGirl.create :vehicle
end
it "should work on vehicle path" do
visit vehicle_path(@vehicle)
expect(page).to have_content @vehicle.name
end
describe "with js", js: true do
it "should work on root path" do
visit root_path
expect(page).to have_content "My Root"
end
it "should work on vehicle path" do
expect(Vehicle.find(@vehicle.to_param)).to be_present # no error
visit vehicle_path(@vehicle) # error: ActiveRecord::RecordNotFound in controller from Vehicle.find (same as above line)
expect(page).to have_content @vehicle.name
end
end
Here is my pared-down spec_helper.rb:
require 'rubygems'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl_rails'
require 'database_cleaner'
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara/poltergeist'
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new app, window_size: [1600, 1200], js_errors: false
end
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.order = "random"
Capybara.javascript_driver = :poltergeist
config.include Capybara::DSL
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation) # moving to before :each doesn't help
DatabaseCleaner.strategy = :truncation # moving to before :each doesn't help
end
config.around :each do |example| # refactoring as before/after with .start/.clean doesn't help
DatabaseCleaner.cleaning { example.run }
end
end
source to share
I created a new project and basically copied your setup and it seems to work fine - you can see it here - http://github.com/twalpole/demo_truncation - not sure what the difference is with your setup and trying to use a new app
source to share