11

我正在使用以下宝石和ruby-1.9.3-p194

  • 导轨 3.2.3

  • rspec-rails 2.9.0

  • 叉叉 1.0.0rc2

  • 卫矛 0.6.1

此Gemfile.lockGemfile中提供了已用 gem 的完整列表。

我正在使用这个配置文件:

如果我修改任何模型(或自定义验证器app/validators等),重新加载代码将不起作用。

我的意思是当我运行规范时(在守卫控制台上按 Enter 键)Spork 包含“旧代码”并且我收到过时的错误消息。但是当我手动重新启动 Guard and Spork (CTRC-C CTRL-d guard) 时,一切正常。但是几次之后就累了。

问题:

有人可以查看我的配置文件并修复阻止更新代码的错误。

或者这可能是最新 Rails 版本的问题?


PS 这个问题在一些项目上反复出现(在一些项目上不重复)。但我还没有弄清楚为什么会发生这种情况。

PS2 也许这个问题与此有关ActiveAdminapp/admin当我在代码中更改文件时会重新加载。

4

7 回答 7

8

解决方法:

# config/environments/test.rb
config.cache_classes = false

但它是“双刃剑”。

规格现在运行时间长约 2.0 倍。但它仍然比一次又一次地重新启动 Spork 快。


更新 28.06.2013

使用宙斯。它完美地工作。基准在底部..

如果您正在使用1.9.3考虑安装真正加快加载应用程序的特殊补丁。

RVM 补丁集

rbenv 指令

背景与基准:

我有一个相当大的1.9.3应用程序,我想加快应用程序加载速度,Spork 不起作用,所以我开始寻找其他解决方案:

我写了一个空规范来查看加载我的应用程序需要多长时间

-/spec/empty_spec.rb

require 'spec_helper'

describe 'Empty' do

end

普通 1.9.3

time rspec spec/empty_spec.rb 64,65s user 2,16s system 98% cpu 1:07,55 total

1.9.3 + rvm 补丁集

time rspec spec/empty_spec.rb 17,34s user 2,58s system 99% cpu 20,047 total

1.9.3 + rvm 补丁集 + 宙斯

time zeus test spec/empty_spec.rb 0,57s user 0,02s system 58% cpu 1,010 total [w00t w00t!]

于 2012-04-26T14:46:16.673 回答
7

或者,您可以为模型、控制器和其他代码添加保护。当这些文件中的任何一个发生更改时,它都会使警卫重新加载 spork。

guard 'spork',
      :rspec_env => {'RAILS_ENV' => 'test'} do
  watch(%r{^app/models/(.+)\.rb$})
  watch(%r{^lib/(.+)\.rb$})
end
于 2012-04-26T14:54:43.630 回答
5

我有同样的问题。测试已重新加载并成功运行以更改 model_spec.rb。当我对 model.rb 文件进行更改时,测试会重新运行,但是代码似乎已被缓存-因此未应用更改。

它需要结合几个答案才能使事情正常进行:

# /config/environments/test.rb
config.cache_classes = !(ENV['DRB'] == 'true')

# spec_helper.rb
Spork.each_run do
  .....
  ActiveSupport::Dependencies.clear
end

我还将 spork 更新到 (1.0.0rc3) 并用 spork-rails 替换了 spork gem,正如上面@23inhouse 所提到的。但是,尽管升级 spork 可能有效果,但我没有看到 gemfile 中的任何一个 gem 之间有任何区别。

Hopefully this helps someone else not spend any more hours banging their head against the wall.

于 2013-07-02T16:23:43.073 回答
4

尽管 Spork 很棒,但它似乎在每次 Rails 升级时都会中断:(

在 Rails 3.2.3 上,我在 spec/spec_helper.rb 中添加了这个片段,以强制重新加载 app 目录中的所有 ruby​​ 文件。

Spork.each_run do
  # This code will be run each time you run your specs.
  Dir[Rails.root + "app/**/*.rb"].each do |file|
    load file
  end
end
于 2012-06-26T09:13:46.890 回答
3

就我而言,问题出在布料上。它不允许 spork 重新加载模型。

Spork.prefork do
  ENV['RAILS_ENV'] ||= 'test'

  # Routes and app/ classes reload
  require 'rails/application'
  Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
  Spork.trap_method(Rails::Application, :eager_load!)

  # Draper preload of models
  require 'draper'
  Spork.trap_class_method(Draper::System, :load_app_local_decorators)

  # Load railties
  require File.expand_path('../../config/environment', __FILE__)
  Rails.application.railties.all { |r| r.eager_load! }
  ...

只需记住在加载环境之前为 Draper 插入陷阱方法。

于 2012-07-31T17:21:47.500 回答
2

Spork 得到了清理,并提取了一些功能。

https://github.com/sporkrb/spork-rails

将此添加到您的 Gemfile

gem 'spork-rails'

于 2013-01-25T02:03:06.977 回答
0

通过向 spork.each_run 方法添加更多内容来修复相同的问题。

导轨 3.2.2

另外,我建议一次运行一个测试。它速度更快,噪音更小,而且我们通常一次只进行一项测试。

rspec spec -e 'shows answer text'

我发现它比使用 Guard 更快、更容易,因为我只是坐在那里等待 Guard 完成。此外,当我进行更改时,Guard 并不总是重新加载正确的文件并运行正确的测试。

spec_helper.rb 文件:

require 'spork'

Spork.prefork do
  ENV['RAILS_ENV'] ||= 'test'

  require File.expand_path('../../config/environment', __FILE__)
  require 'rspec/rails'
  require 'rspec/autorun'
  require 'capybara/rspec'
  require 'capybara/rails'

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
end

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

  RSpec.configure do |config|
    # config.mock_with :mocha
    # config.mock_with :flexmock
    # config.mock_with :rr
    config.mock_with :rspec

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
    config.fixture_path = "#{::Rails.root}/spec/fixtures"

    # If you're not using ActiveRecord, or you'd prefer not to run each of your
    # examples within a transaction, remove the following line or assign false
    # instead of true.
    config.use_transactional_fixtures = true

    # If true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
    config.infer_base_class_for_anonymous_controllers = false

    config.include RequestHelpers, :type => :request

    config.before :suite do
      DatabaseCleaner.strategy = :truncation
      DatabaseCleaner.clean_with :truncation
    end

    config.before :each do
      DatabaseCleaner.start
    end

    config.after :each do
      DatabaseCleaner.clean
    end

    config.include(MailerHelpers)
    config.before(:each) { reset_email }
  end
  # This code will be run each time you run your specs.
end
于 2012-08-12T20:06:19.440 回答