本文介绍了Capybara +具有数据库清理程序的Poltergeist找不到FactoryGirl记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

我确实将 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项目。在测试新的未经编辑的脚手架时。

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.

为什么Poltergeist找不到我的记录?由于我已经提出了任何建议,因此会有帮助在这里呆了几个小时。

Why aren't my records found by Poltergeist? Any suggestions would be helpful, since I've been at this for hours.

前两个测试通过,而最后一个失败在第二行使用 RecordNotFound

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

这是我精简的spec_helper.rb:

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


推荐答案

我创建了一个新项目,并基本上复制了您的设置,它看起来运行良好-您可以看到在这里--不确定您的设置有什么区别并尝试使用新的应用程序

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

这篇关于Capybara +具有数据库清理程序的Poltergeist找不到FactoryGirl记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 15:36