ruby-on-rails-3 - Rspec 头痛 : configuration advice appreciated

标签 ruby-on-rails-3 postgresql rspec selenium-webdriver capybara

我这辈子似乎无法以一种不会不时抛出随机错误的方式设置 rspec/capybara。我最常见的错误(但不一致)如下:

An error occurred in an after hook
  ActiveRecord::RecordNotFound: Couldn't find Post with id=1
  occurred at [...]/gems/activerecord-3.2.11/lib/active_record/relation/finder_methods.rb:341:in `find_one'

紧随其后的是与预期在规范中创建但后来找不到的模型对象相关的任何事情。例如像

Sorter should populate featured_posts with appropriate posts
Failure/Error: Sorter.top_4.map(&:id).should eq(([best_new_post, best_old_post] + new_posts.last(2).reverse).map(&:id))

   expected: [2, 1, 40, 38]
        got: []

当我单独运行这些测试时,它们总是会通过,而当我在套件中运行时,它们并不总是会失败。我假设线程和数据库的设置方式存在某种问题,但我似乎无法弄清楚。

我的 spec_helper.rb 是来自各种来源的建议的大杂烩:

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'
  require 'capybara/rspec'
  require 'capybara/email/rspec'
  include ApplicationHelper

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  include Warden::Test::Helpers
  Warden.test_mode!

  ActionController::Base.asset_host = "http://localhost:3000"

  RSpec.configure do |config|
    config.include Rails.application.routes.url_helpers

    config.use_transactional_fixtures = false
    config.use_transactional_examples = false #factoryGirl

    # From http://railsapps.github.io/tutorial-rails-devise-rspec-cucumber.html
    config.before(:suite) do
      DatabaseCleaner.strategy = :truncation
      DatabaseCleaner.clean_with(:truncation)
    end
    config.before(:each) { DatabaseCleaner.start }
    config.after(:each) { DatabaseCleaner.clean }
    #

    config.infer_base_class_for_anonymous_controllers = false

    ## Setting up email helpers
    config.include MailerMacros
    config.before(:each) { reset_email }

    ## Setting up test and login helpers for Devise testing
    config.include Devise::TestHelpers, type: :controller
    config.extend ControllerMacros, type: :controller

    # From http://railscasts.com/episodes/413-fast-tests
    config.order = "random"

    config.include FactoryGirl::Syntax::Methods

    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.filter_run focus: true
    config.run_all_when_everything_filtered = true

    config.filter_run_excluding :slow unless ENV["SLOW_SPECS"]

    config.before(:all) { DeferredGarbageCollection.start }
    config.after(:all) { DeferredGarbageCollection.reconsider }
  end
end

Spork.each_run do  
  Rails.cache.clear
  Warden.test_reset!
  FactoryGirl.reload
end

我已经尝试删除部分以进行故障排除,但很难判断它是否解决了任何问题,因为错误是零星的。

这里是否有任何明显的原因导致上述错误,或者关于如何解决此问题的任何建议?

最佳答案

在你的 spec_helper.rb 中,尝试设置

config.use_transactional_fixtures = true

关于ruby-on-rails-3 - Rspec 头痛 : configuration advice appreciated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18069048/

相关文章:

ruby-on-rails - 在 View 中显示缺失的 I18n 翻译

postgresql - Squeryl 中的显式 jsonb 类型转换

ruby-on-rails - 绝对测试所有内容是一种好习惯还是浪费时间(Rails)?

ruby-on-rails - 运行 Rspec 时出现 "No such file to load"错误

ruby - Rspec:如何测试自己的产量

ruby-on-rails - 在 yaml 文件中传递变量

ruby - Gemfile - 将生产 gems 与开发 gems 分开

javascript - Rails 3.1.1 中的 jquery 总是给出 DOMException

java - 在push heroku java中写入对象时占用太多mib

sql - 为什么计数结果与正常选择行计数不同?在极光 Postgresql 中