【问题标题】:Spork & Rails 3.1 engine: helpers are not reloadedSpork & Rails 3.1 引擎:助手不会重新加载
【发布时间】:2012-09-24 11:54:11
【问题描述】:

我正在开发一个 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 会。可能是因为 helpers 是模块,而测试不依赖于模块,而是依赖于实现模块的类,所以模块被正确地重新加载但没有正确混合??

目前,我只是将所有初始化程序代码放入 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

【问题讨论】:

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


    【解决方案1】:

    我对这个话题做了很多研究,我在这两篇博文中写过:

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

    这是我当前引擎的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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-24
      • 2012-05-04
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      相关资源
      最近更新 更多