我正在使用 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/