gpt4 book ai didi

ruby-on-rails-3 - 使用 rake 从 db/seeds.rb 自动加载种子数据

转载 作者:行者123 更新时间:2023-11-28 19:39:41 25 4
gpt4 key购买 nike

我正在使用 rails-rspec gem,我有几个规范(模型、 Controller 等)。当我运行时:

bundle exec rake

一切都经过测试。但是,我想通过在数据库创建后(在测试环境中)播种一些数据(来自 db/seeds.rb)来改进我的规范。

我的 spec/spec_helper.rb 文件如下所示:

ENV["RAILS_ENV"] ||= 'test'

require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
require 'ruby-debug'

# 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}

RSpec.configure do |config|
config.mock_with :rspec

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

# 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 = false

config.include SpecHelper

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

config.before(:each) do
DatabaseCleaner.start
stub_xmpp_rest_client!
end

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

config.include Devise::TestHelpers, :type => :controller
config.include Delorean
config.after(:each) { back_to_the_present }
config.include Factory::Syntax::Methods
config.extend ControllerMacros, :type => :controller
end

最好的方法是什么?谢谢。

最佳答案

坏主意!永远不要为您的测试数据库播种。使用工厂在每个测试中创建该测试通过所需的记录。为测试数据库植入种子会使您的测试不那么可靠,因为您将做出许多未在测试中明确说明的假设。

关于ruby-on-rails-3 - 使用 rake 从 db/seeds.rb 自动加载种子数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8386604/

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