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

我确实将 use_transactional_fixtures 设置为 false 并将 DatabaseCleaner.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

最佳答案

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

关于ruby-on-rails - Capybara + Poltergeist 与数据库清理器找不到 FactoryGirl 记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31751287/

10-11 03:01