2012-09-24 111 views
0

我正在開發Rails 3.1引擎,並且想要對其進行測試。我爲此使用RSpec,並且一切都很好,但是當試圖使用Spork時,我遇到的問題是我的幫助程序沒有正確重新加載。Spork&Rails 3.1引擎:幫助程序不會重新加載

我讀了很多關於高配車型類似的問題,我想出了以下可能的解決辦法:

# 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重新啓動。也許這是因爲助手是模塊,測試不依賴於模塊,而是依賴於實現模塊的類,所以這些模塊被正確地重新加載,但沒有正確地混合在?

在暫時的,我只是把所有intitializer代碼到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 

回答

0

我已經做了很多研究這個話題,我已經在這兩個博客寫帖子:

特別是關於我上面的問題:我可以想象它不起作用,因爲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