gpt4 book ai didi

ruby-on-rails - 如何在错误的rspec规范之间清理数据库?

转载 作者:行者123 更新时间:2023-12-04 06:14:01 24 4
gpt4 key购买 nike

我已将database_cleaner gem添加到我的rails应用程序中,以便在两次规范之间清理我的数据库。这是我当前在spec/spec_helper.rb中对database_cleaner的配置:

  config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
DatabaseCleaner.start
DatabaseCleaner.clean
end

config.before(:each) do
DatabaseCleaner.clean
end

config.after(:each) do
DatabaseCleaner.clean
end

config.after(:suite) do
DatabaseCleaner.clean
end

现在,只要最后运行的所有规范通过或失败,此配置就可以正常工作。

但是,在发生错误的情况下(rspec不会像minitest那样为您提供漂亮的 E小代码,它会抛出此类错误:
09:17:32 - INFO - Running: spec
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/gems/activerecord-4.0.1/lib/active_record/validations.rb:57:in `save!': Validation failed: Email has already been taken (ActiveRecord::RecordInvalid)

),则不会清除数据库!错误即将保留在数据库中之前,来自规范的剩余数据。我想这是因为database_cleaner不会认为错误的规范已经完成,因此也不会清理数据库。

现在,直到再次运行规范,这才不会真正造成任何危害。然后,残留数据将导致类似于以下的错误:
09:17:32 - INFO - Running: spec
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1/gems/activerecord-4.0.1/lib/active_record/validations.rb:57:in `save!': Validation failed: Email has already been taken (ActiveRecord::RecordInvalid)

解决这个错误很简单;运行 rails_env=test rake db:reset或启动数据库 shell 并使用sql语句清空相关表将清除此数据,并使规范运行顺利。

但是,这变得烦人了。我的任何一个规范中的一个错误角色(任何使其成为错误而非失败的事物)都会导致我的整个测试工作流程陷入困境,就像自动武器的发射机制一样!

您对database_cleaner有什么建议?您是否有任何示例配置,即使在测试错误的情况下也可以清理数据库?

我正在使用Guard运行我的rspec,并用factory-girl进一步增强了它们:

gem 文件:
source 'https://rubygems.org'

group :development do
gem 'capistrano'
gem 'rb-fsevent'
gem 'debugger'
end

group :development, :test do
gem 'rspec-rails', '~> 2.14.0'
gem 'sqlite3'
gem 'guard-rspec'
gem 'guard-livereload', require: false
gem 'guard-shell'
gem 'webrick', '~> 1.3.1'
end

group :test do
gem 'factory_girl_rails'
gem 'capybara', '~> 2.2.0'
gem 'selenium-webdriver'
# capybara-webkit gem requires an application called 'libqtwebkit-dev' to build. To install 'libqtwebkit-dev' in Ubuntu, run
# sudo apt-get install libqtwebkit-dev
# gem 'capybara-webkit'
gem 'rb-readline'
gem 'launchy'
gem 'database_cleaner'
end

group :production do
gem 'pg'
# gem 'puma'
end

# rails version
gem 'rails', '4.0.1'

# standard library
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'


group :doc do
gem 'sdoc', require: false
end

# custom
gem 'activeadmin', github: 'gregbell/active_admin'
gem 'devise'
gem 'simple_form'

spec/spec_helper:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
config.include Capybara::DSL

config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
DatabaseCleaner.start
DatabaseCleaner.clean
end

config.before(:each) do
DatabaseCleaner.clean
end

config.after(:each) do
DatabaseCleaner.clean
end

config.after(:suite) do
DatabaseCleaner.clean
end

# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# config.include RSpec::Rails::RequestExampleGroup, type: :feature

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end

最佳答案

你想改变这个

config.after(:suite) do
DatabaseCleaner.clean
end

对此:
config.after(:suite) do
DatabaseCleaner.clean_with(:truncation)
end

否则,它将简单地回滚事务,这将保留事务开始之前存在的所有数据。

关于ruby-on-rails - 如何在错误的rspec规范之间清理数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20295927/

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