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

标签 ruby-on-rails-3 testing rspec rake

我正在使用 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/

相关文章:

ruby - 使用 Capybara 或 Xpath 查找表中给定列中是否存在内容?

testing - 通过 QTP/UFT 使用 abs_x/abs_y 或 x/y(相对)进行 WebElement 识别有多可靠?

android - 如何在 Android Studio 0.2.x 中编写/构建测试用例

ruby-on-rails - 如何在 Rails 中捕获 ffmpeg 输出?

ruby-on-rails - 使用反引号从 Rails 应用程序中启动另一个 Rails 服务器

eclipse - 是否存在用于生成 Java 测试类的 Maven 插件?

ruby-on-rails - RSpec 在 controller_spec 中将对象作为参数传递

rspec - 看不到我在iterm2中输入的内容

ruby - Rails 3.2.6 路线不再有效

ruby-on-rails-3 - 向 Rails 3 应用程序添加作用域会扰乱 Devise 变量