Spork& Rails 3.1引擎:帮助程序不会重新加载

时间:2012-09-24 11:54:11

标签: ruby-on-rails rspec rails-engines spork

我正在开发一个Rails 3.1引擎并想测试它。我使用RSpec,一切都运行良好,但在尝试使用Spork时,我遇到的问题是我的助手没有正确重新加载。

我读了很多关于模型的类似问题,我想出了以下可能的解决方法:

# my_engine/spec/spec_helper.rb

ActiveSupport::Dependencies.clear
ActiveRecord::Base.instantiate_observers

Dir[File.join(File.dirname(__FILE__), '..', 'app', 'helpers', '*.rb')].each do |file|
  require file
end

# my_engine/spec/dummy/config/environments/test.rb
Dummy::Application.configure do
  # ...
  config.cache_classes = !(ENV['DRB'] == 'true') # Ensure that classes aren't cached when using Spork.
  # ...
end

这肯定会重新加载任何帮助文件(我添加了一个断点来检查这个),但是更改没有在测试中反映出来,只是重新启动了Spork。也许是因为帮助程序是模块,并且测试不依赖于模块,而是依赖于实现模块的类,因此模块正确地重新加载但没有正确地混合在一起?

目前,我只是将所有初始化代码放入each_run块中:

# Configure Rails Environment
ENV["RAILS_ENV"] = "test"

require File.expand_path("../dummy/config/environment.rb", __FILE__)
require 'rspec/rails'

Rails.backtrace_cleaner.remove_silencers!

# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }

RSpec.configure do |config|
  config.use_transactional_fixtures = true

  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.filter_run :focus => true
  config.run_all_when_everything_filtered = true
end

1 个答案:

答案 0 :(得分:0)

我已经就这个主题做了很多研究,我在这两篇博文中写过:

特别针对上面的问题:我可以想象它没有用,因为我require 'rspec/autorun'块中没有Spork.prefork,但我不完全确定。

这是我当前引擎的spec_helper.rb(根据需要成功重新加载所有内容):

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../dummy/config/environment", __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'

  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    config.use_transactional_fixtures = true
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.infer_base_class_for_anonymous_controllers = false
    config.treat_symbols_as_metadata_keys_with_true_values = true
    config.filter_run :focus => true
    config.run_all_when_everything_filtered = true
  end
end

Spork.each_run do
  # This code will be run each time you run your specs.
end