1

我不能爲我的生活似乎設置rspec /水豚不會拋出隨機錯誤的方式。我最常見的錯誤(但不是一致)是沿着線:Rspec頭疼:配置建議讚賞

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 

我試圖消除件來解決,但很難衡量其是否HSS解決任何事情,因爲這些錯誤是零星的。

這裏有什麼突出的上述錯誤的明顯原因,或任何有關如何排除故障的建議?

+0

是否遵循下https://github.com/thoughtbot/capybara-webkit#usage安裝/配置說明? –

+0

我猜這是*可能*,你的鉤子順序是不可靠的,儘管我希望配置鉤子在規範鉤子之後觸發。嘗試爲每個鉤子記錄日誌,然後看看命令是否對'丟失'的對象產生了扭曲。 –

+1

編輯:剛剛發現[這水豚問題](https://github.com/jnicklas/capybara/issues/1089)這可能會揭示一些東西 –

回答

1

在你spec_helper.rb,嘗試設置

config.use_transactional_fixtures = true 
+0

我繼續前進,並給出了一個鏡頭 - 它在webdriver/js部分中拋出各種錯誤。你在這裏有什麼想法? – tyler