ruby-on-rails - Capybara + Poltergeist with Database-cleaner 找不到 FactoryGirl 记录

标签 ruby-on-rails ruby rspec capybara poltergeist

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

我确实将 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 with Database-cleaner 找不到 FactoryGirl 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31751287/

相关文章:

ruby - 使用 DO End 循环时出现随机枚举器错误

ruby-on-rails - Rails Friendly Id - 找不到 id=electronics 的类别

ruby - 嵌套哈希定义?()

ruby-on-rails - rails 3 simple_form_for :defaults

ruby - 如何创建/编辑/配置多个 PayPal 标准支付按钮?

arrays - 为 RSpec 数组匹配器启用差异

ruby-on-rails-3 - 如何允许异常在 rspec 请求规范中冒泡

ruby-on-rails - RSpec 测试不调用 Controller

ruby-on-rails - ActiveRecord::Base:Class 的未定义方法 `mass_assignment_sanitizer=' (NoMethodError)

ruby-on-rails - 如何在 Sidekiq 上为 Redis 6 启用 TLS?