gpt4 book ai didi

ruby-on-rails - Rails,成为 friend 并在测试套件之间重置数据库

转载 作者:行者123 更新时间:2023-12-04 02:22:56 27 4
gpt4 key购买 nike

工作正在从 Rails 3 过渡到 Rails 4。在开发方面,一切似乎都或多或少地顺利运行,但是当所有套件(单元、功能和集成)同时运行时,测试会导致多种不同的失败rake:test .

有趣的是,单独运行这些套件中的每一个都不会产生故障。这强烈表明在 Rails 4 中为我们的测试重置数据库并没有像在 Rails 3 中那样重置。

我尝试覆盖 rake:test 任务来执行 db:test:prepare在运行每个套件之前,但这显然没有做我认为它所做的,或者更确切地说,我想要它做的,即为每个测试使用一组新数据,因此独立于每个测试成功或失败其他测试 - 在我们的 Rails 3 设置中(曾经或应该是)的方式。

为什么会这样?我该如何解决?

(请注意,测试框架是带有夹具等的 vanilla Rails。我们得到的测试失败通常是外键错误或数据以预期方式更改的失败,这些错误在测试之前重置数据库时不会出现套房。)

编辑:

这是我对 Rakefile 所做的更改。这个想法是让 rake:test 像在 Rails 3 中一样执行,除了在单元、功能和集成套件之间正确重置数据库。

# /Rakefile

task :test => [];
Rake::Task[:test].clear
task :test do
puts Rails.env
if Rails.env == "test"
puts "units"
# Rake::Task["db:drop"].execute - results in 'test database not configured' error
# Rake::Task["db:reset"].execute - results in 'relation 'table' does not exist' error
Rake::Task["db:create"].execute
Rake::Task["db:migrate"].execute
Rake::Task["test:units"].execute
puts "functionals"
Rake::Task["db:schema:load"].execute
Rake::Task["test:functionals"].execute
puts "integration"
Rake::Task["db:schema:load"].execute
Rake::Task["test:integration"].execute
end
end

编辑2:

Rails 版本 4.1.8

这是我的 test_helper。我省略了对我们的数据不起作用的辅助方法,因为我可能不允许共享它们,而且它们与测试数据库的加载方式无关。

另请注意,我们还有一个自定义 schema.rb,从中生成测试数据,因为尝试从迁移创建它们不支持我们在最终产品中想要的某些自定义功能/行为,例如物化 View 。编辑后的内容将包含在 <<>> 中。
#test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
include ActionDispatch::TestProcess

class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
set_fixture_class <<cached_object>>: ResultsDb::<<DesiredObjectClass>>Cache
fixtures :all

# Add more helper methods to be used by all tests here...
def reload_contig(contig_sym)
contig = contigs(contig_sym)

src = contig.src
new_tig = Contig.new(
org: contig.org,
version: contig.version,
size: contig.size,
sha1: contig.sha1,
active: contig.active,
src_id: src.id,
src_type: contig.src_type)

new_tig.id = contig.id
contig.destroy
unless new_tig.save
raise new_tig.errors.full_messages.inspect
end
src.contigs << new_tig
src.save
new_tig
end

def reload_coord(coord_sym)
coord = coords(coord_sym)

new_coord = Coord.new(
:mapped_id => coord.mapped_id,
:mapped_type => coord.mapped_type,
:contig_id => coord.contig_id,
:contig_type => coord.contig_type,
:start => coord.start,
:stop => coord.stop,
:fwd_strand => coord.fwd_strand
)
new_coord.id = coord.id
coord.destroy
new_coord.save!
end
end

最佳答案

您可能会查看 DatabaseCleaner gem 专门设计用于帮助您在 Rails 中进行测试期间保持数据库的一致性和清洁度。

当我的代码绕过 ActiveRecord 并直接操作数据时,我只会遇到问题。在所有其他情况下,这个 gem 非常适合我进行测试。

关于ruby-on-rails - Rails,成为 friend 并在测试套件之间重置数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30470335/

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