Capybara + Poltergeist与Database-cleaner没有找到FactoryGirl记录

时间:2015-07-31 16:50:49

标签: ruby-on-rails ruby rspec capybara poltergeist

我正在编写集成测试并使用FactoryGirl创建记录。当测试具有RecordNotFound(使用Poltergeist)时,我的控制器正在抛出js: true,即使它们是在非js测试中找到的(没有Poltergeist)。

我确实use_transactional_fixtures设置为falseDatabaseCleaner.strategy设置为:truncation,这似乎涵盖了此问题上每个现有的SO问题。我尝试用Selenium(用Firefox)代替Poltergeist,但我得到了相同的结果。 ETA 我使用RSpec-Rails 3.3.3,Capybara 2.4.4,Poltergeist 1.6.0,PhantomJS 1.9.8,Database Cleaner 1.4.1启动了一个全新的Rails 4.2.3项目并得到了相同的结果在测试一个新的未经编辑的脚手架时。

为什么我的记录没有被Poltergeist发现?任何建议都会有所帮助,因为我已经在这里工作了好几个小时。

前两个测试通过,而最后一个测试在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

这是我的精简版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 个答案:

答案 0 :(得分:2)

我创建了一个新项目并基本上复制了您的设置,它似乎工作正常 - 您可以在此处看到它 - http://github.com/twalpole/demo_truncation - 不确定您的设置与尝试新应用程序的区别是什么