Capybara + Poltergeist with Database-cleaner not finding FactoryGirl records

懵懂的女人 提交于 2019-12-23 17:24:51

问题


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:


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



来源:https://stackoverflow.com/questions/31751287/capybara-poltergeist-with-database-cleaner-not-finding-factorygirl-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!