我有一個Spork測試服務器的問題。rspec,factory_girl和datamapper spork和cache_classes問題
如果我在config/environments/test.rb中設置了config.cache_classes = false,那麼規格開始出現rasie錯誤。
Failure/Error: task = Factory(:something, :foo => @foo, :bar => @bar)
DataMapper::ImmutableError:
Immutable resource cannot be modified
這是我spec_helper.rb:
require 'spork'
Spork.prefork do
if ENV['CODE_COVERAGE'] == '1'
require 'simplecov'
SimpleCov.start 'rails'
end
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'webmock/rspec'
require 'factory_girl'
Dir[Rails.root.join("spec/controllers/shared/*.rb")].each { |f| require f }
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
config.mock_with :mocha
config.include Rails.application.routes.url_helpers
config.include UrlHelper
config.before(:each) do
repository(:default) do
transaction = DataMapper::Transaction.new(repository)
transaction.begin
repository.adapter.push_transaction(transaction)
end
end
config.after(:each) do
repository(:default).adapter.pop_transaction.try(:rollback)
end
end
end
# This code will be run each time you run your specs.
Spork.each_run do
# reload factories
Factory.definition_file_paths = Dir[File.join(Rails.root, "spec", "factories")]
Factory.find_definitions
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean
LoggedEvent.all.destroy!
end
當我有config.cache_classes = true,則一切正常,但它不會重新加載我一個模型,控制器類,所以我不在這種情況下,請注意使用spork。
我試圖添加到spec_helper.rb這樣的事情,當緩存爲真:
Spork.each_run do
Dir.glob("#{Rails.root}/app/models/*.rb").sort.each { |file| load file }
end
但我不喜歡這種解決方案。
等待,ActiveSupport :: Dependencies.clear屬於prefork或each_run嗎?我看到了相互衝突的故事。這兩者似乎都不適合我,都會導致加載問題:「預計xxxx會定義xxxx」。另外,單獨解決問題是否是'ActiveSupport :: Dependencies.clear'?還是需要與原始問題中的each_run示例結合使用? – 2011-11-14 15:20:44
我不知道爲什麼,但我的項目只是突然開始這樣做,沒有明顯的原因。我最終將Dependencies.clear放入我的Spork.each_run中,並且它對我有用。我沒有我的Rspec.config在each_run中,只是:FactoryGirl.reload和ActiveSupport :: Dependencies.clear。希望有所幫助。 – 2011-11-29 18:58:47
ActiveSupport :: Dependencies.clear應該添加在prefork的末尾,而不是each_run – Ran 2011-12-01 09:48:36