我不能因为我的生活似乎以不会随时抛出随机错误的方式设置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
我已经尝试删除碎片进行故障排除,但由于错误是零星的,所以很难判断它是否解决了任何问题。
这里有什么突出显示上述错误的明显原因,或者有关如何解决此问题的任何建议?
答案 0 :(得分:1)
在spec_helper.rb中,尝试设置
config.use_transactional_fixtures = true