gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:33:45 25 4
gpt4 key购买 nike

我这辈子似乎无法以一种不会不时抛出随机错误的方式设置 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com