我的 Spork 测试服务器有问题。
如果我在 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 没有意义。
当缓存为 true 时,我尝试向 spec_helper.rb 添加类似的内容:
Spork.each_run do
Dir.glob("#{Rails.root}/app/models/*.rb").sort.each { |file| load file }
end
但我不喜欢这个解决方案。
只需添加:
ActiveSupport::Dependencies.clear
到预分叉块的末尾。这将负责清除模型。
另外,您希望将 rspec 配置包含移动到 Spork.each_run,同样需要规范支持和共享文件。
这很有效,我在 2 个项目中使用了这个设置,没有任何麻烦。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)