使用 rake 从 db/seeds.rb 自动加载种子数据 [英] Auto-load the seed data from db/seeds.rb with rake
问题描述
我正在使用 rails-rspec
gem 并且我有几个规格(模型、控制器等).当我跑步时:
I'm using rails-rspec
gem and I have several specs (models, controllers, etc). When I run:
bundle exec rake
一切都经过测试.但是,我想在创建数据库后(在测试环境中)通过播种一些数据(来自 db/seeds.rb)来改进我的规格.
everything is tested. However, I would like to improve my specs by seeding some data (from db/seeds.rb) just after the database is created (in test environment).
我的 spec/spec_helper.rb 文件如下所示:
My spec/spec_helper.rb file looks like this:
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
什么是最好的方法?谢谢.
What could do the best way to do so? Thanks.
推荐答案
根据您的种子文件的配置方式,您可能只能从 before(:each)
加载/运行它或 before(:all)
块:
Depending on how your seed file is configured, you might just be able to load/run it from a before(:each)
or before(:all)
block:
load Rails.root + "db/seeds.rb"
这篇关于使用 rake 从 db/seeds.rb 自动加载种子数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!