简体   繁体   English

Spork不会重新加载代码

[英]Spork doesn't reload code

I am using following gems and ruby-1.9.3-p194 : 我正在使用以下宝石和ruby-1.9.3-p194

  • rails 3.2.3 轨道3.2.3

  • rspec-rails 2.9.0 rspec-rails 2.9.0

  • spork 1.0.0rc2 spork 1.0.0rc2

  • guard-spork 0.6.1 警卫队0.6.1

Full list of used gems is available in this Gemfile.lock or Gemfile . Gemfile.lockGemfile中提供了已使用gems的完整列表。

And I am using this configuration files: 我正在使用此配置文件:

If I modify any model (or custom validator in app/validators etc) reloading code doesnt works. 如果我修改任何模型(或app/validators自定义验证app/validators等),重新加载代码不起作用。

I mean when I run specs (hit Enter on guard console) Spork contain "old code" and I got obsolete error messages. 我的意思是当我运行规格(在防守控制台上按Enter键)Spork包含“旧代码”,我得到过时的错误消息。 But when I manually restart Guard and Spork (CTRC-C CTRL-d guard) everything works fine. 但是当我手动重启Guard和Spork(CTRC-C CTRL-d后卫)时,一切正常。 But it is getting tired after few times. 但几次后它已经累了。

Questions: 问题:

Can somebody look at my config files please and fix error which block updating code. 有人可以看看我的配置文件并修复阻止更新代码的错误。

Or maybe this is an issue of newest Rails version? 或者这可能是最新Rails版本的问题?


PS This problem repeats and repeats over some projects (and on some NOT). PS这个问题在一些项目中重复并重复(在某些项目上)。 But I haven't figured out yet why this is happens. 但我还没弄清楚为什么会发生这种情况。

PS2 Perhaps this problem is something to do with ActiveAdmin ? PS2也许这个问题与ActiveAdmin When I change file in app/admin code is reloaded. 当我在app/admin代码中更改文件时重新加载。

Workaround: 解决方法:

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

But it is "double-edged sword". 但这是“双刃剑”。

Specs run now ~2.0x time longer. 规格现在运行时间延长了2.0倍。 But it is still faster than restarting again and again Spork. 但它仍然比一次又一次重启Spork更快。


Update 28.06.2013 更新于28.06.2013

Use Zeus . 使用宙斯 It works perfectly. 它完美地运作。 Benchmarks are at the bottom.. 基准位于底部..

If you are using 1.9.3 consider installing special patches which REALLY speed up loading app. 如果您使用的是1.9.3考虑安装特殊的补丁,这样可以加快加载应用程序的速度。

RVM patchsets RVM补丁集

rbenv instructions rbenv说明

Background & Benchmark: 背景与基准:

I have a quite large 1.9.3 app and I wanted to speedup app loading, Spork doesn't work so I started looking for other solutions: 我有一个非常大的1.9.3应用程序,我想加速应用程序加载,Spork不工作,所以我开始寻找其他解决方案:

I write a empty spec to see how long it takes to load my app 我写了一个空的规范,看看加载我的应用需要多长时间

-/spec/empty_spec.rb - /规格/ empty_spec.rb

require 'spec_helper'

describe 'Empty' do

end

plain 1.9.3 平原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 patchsets 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 patchsets + zeus 1.9.3 + rvm patchsets + zeus

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

Alternatively, you can add guards for your models, controllers and other code. 或者,您可以为模型,控制器和其他代码添加防护。 It'll make guard reload spork when any of these files change. 当任何这些文件发生变化时,它会使警卫重新加载spork。

guard 'spork',
      :rspec_env => {'RAILS_ENV' => 'test'} do
  watch(%r{^app/models/(.+)\.rb$})
  watch(%r{^lib/(.+)\.rb$})
end

I had the same problem. 我有同样的问题。 Tests were reloaded and ran successfully for changes to model_spec.rb. 重新加载测试并成功运行以更改model_spec.rb。 When I made changes to the model.rb file the tests were re-run, however the code seemed to be cached - so the changed were not applied. 当我对model.rb文件进行更改时,重新运行了测试,但代码似乎被缓存 - 因此未应用更改。

It required a combination of a few answers to get things working: 它需要一些答案的组合才能使事情顺利进行:

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

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

I also updated spork to (1.0.0rc3) and replaced the spork gem with spork-rails, as mentioned by @23inhouse above. 我还将spork更新为(1.0.0rc3)并用spork-rails替换了spork gem,如上面的@ 23inhouse所述。 However, I did not see any difference between either gem in the gemfile although upgrading spork may have had an effect. 但是,我没有看到gemfile中的任何一个gem之间有任何区别,尽管升级spork可能会产生影响。

Hopefully this helps someone else not spend any more hours banging their head against the wall. 希望这有助于其他人不再花费更多时间撞到墙上。

Great as Spork is, it seems to break on every Rails upgrade :( 伟大的Spork,它似乎打破每个Rails升级:(

On Rails, 3.2.3, I've added this snippet in spec/spec_helper.rb to forcibly reload all ruby files in the app directory. 在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

In my case the problem was draper. 就我而言,这个问题很糟糕。 It didn't allow spork to reload the models. 它不允许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! }
  ...

Just remember to insert the trap method for Draper before loading the environment. 请记住在加载环境之前插入Draper的陷阱方法。

Spork got cleaned up and some functionality was extraced. Spork得到了清理,并提供了一些功能。

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

add this to your Gemfile 将其添加到您的Gemfile中

gem 'spork-rails'

Fixed the same problem by adding more to the spork.each_run method. 通过向spork.each_run方法添加更多内容来修复同样的问题。

Rails 3.2.2 Rails 3.2.2

Also, I recommend running one test a time. 另外,我建议一次运行一次测试。 It's much faster, less noisy, and we normally work on one test at a time anyway. 它速度更快,噪音更小,而且我们通常一次只能进行一次测试。

rspec spec -e 'shows answer text'

I find it is faster and easier than using Guard because I was just sitting around waiting for Guard to finish. 我发现它比使用Guard更快更容易,因为我只是坐在那里等待Guard完成。 Also, Guard did not always reload the right files and run the right tests when I made a change. 此外,当我做出更改时,Guard并不总是重新加载正确的文件并运行正确的测试。

spec_helper.rb file: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM