4
votes

I'm writing integration tests and creating records with FactoryGirl. My controllers are throwing RecordNotFound when the test has js: true (with Poltergeist) even though they are found in a non-js test (without Poltergeist).

I do have use_transactional_fixtures set to false and DatabaseCleaner.strategy set to :truncation, which seemingly covers every existing SO question on this issue. I tried substituting Selenium (with Firefox) for Poltergeist but I get the same result. ETA I started a fresh 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 get the same results when testing a new, unedited scaffold.

Why aren't my records found by Poltergeist? Any suggestions would be helpful, since I've been at this for hours.

The first two tests pass while the last fails 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
1
Do you also have a rails_helper.rb -- if so please postThomas Walpole
@TomWalpole No rails_helper, this is AFAIK all of my spec config (I "inherited" the project from a previous dev).eirikir
try setting the DatabaseCleaner.strategy in a before(:each) rather than :suiteThomas Walpole
@TomWalpole tried that and tried moving clean_with as well, no diceeirikir
I created a fresh project and basically copied over your setup, and it appears to work fine -- you can see it here - github.com/twalpole/demo_truncation - not sure what the difference with your setup and attempt at a fresh app areThomas Walpole

1 Answers

2
votes

I created a fresh project and basically copied over your setup, and it appears to work fine -- you can see it here - http://github.com/twalpole/demo_truncation - not sure what the difference with your setup and attempt at a fresh app are