Spork / Autotest不会自动获取更改

时间:2011-08-30 09:13:46

标签: ruby-on-rails ruby-on-rails-3 autotest spork

我正在按照http://ruby.railstutorial.org/上的教程开始我的第一个rails应用程序。我完全按照教程中的说法设置了我的开发环境(我正在使用带有Lion的Macbook Pro),除了一次打嗝之外它没有任何问题。我首先编写失败的测试然后我对代码进行更改以便它们通过,我可以在浏览器中检查页面是否正常工作但由于某种原因,测试结果仍然是“红色”。我有一个运行3个选项卡的终端,第一个选项卡,我有spork运行(bundle exec spork),第二个选项卡,我有rails服务器运行(rails s)和第三个选项卡,我进行所有命令行更改并运行测试(自动测试) || bundle exec rspec spec /)。我必须重新启动spork,rails服务器,然后再次测试,看看它们是“绿色”。

这是预期的行为吗?因为根据教程,大多数时候我应该能够看到它们变绿,而不必重新启动服务器/ spork。

修改

这是我的spec_helper.rb文件的样子

require 'rubygems'
require 'spork'

Spork.prefork do
  # Loading more in this block will cause your tests to run faster. However,
  # if you change any configuration or code from libraries loaded here, you'll
  # need to restart spork for it take effect.

  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'


  # Requires supporting files with custom matchers and macros, etc,
  # in ./support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    # == Mock Framework
    #
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
    #
    # config.mock_with :mocha
    # config.mock_with :flexmock
    # config.mock_with :rr
    config.mock_with :rspec

    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, comment the following line or assign false
    # instead of true.
    config.use_transactional_fixtures = true
  end

end

Spork.each_run do
  # This code will be run each time you run your specs.
end

感谢您提供建议!

6 个答案:

答案 0 :(得分:17)

我在尝试使用spork和watchr设置环境时遇到了同样的问题,并在此处找到了解决方案(至少在我的情况下):http://www.rubyinside.com/how-to-rails-3-and-rspec-2-4336.html

修改config/environments/test.rb

更改

config.cache_classes = true

config.cache_classes = false

这应该可以在不重新启动的情况下获取对控制器和模型的更改。

编辑:

我遇到了cache_classes设置为false的问题(这导致了机械师和黄瓜的问题 - rspec和机械师很奇怪就好了。)

无论如何,似乎有一个更好的解决方案(取自这里:http://ablogaboutcode.com/2011/05/18/spork-rspec-sham-and-caching-classes/和此处:http://mikbe.tk/2011/02/10/blazingly-fast-tests/),即将以下代码添加到Spork.each_run块

Spork.each_run do
  ActiveSupport::Dependencies.clear
  ActiveRecord::Base.instantiate_observers

  load "#{Rails.root}/config/routes.rb"
  Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }  
end if Spork.using_spork?

答案 1 :(得分:1)

如果您在没有更改的情况下运行测试,它是否有效?我的意思是“接受”你的改变吗?

如果是这种情况,请打开spec_helper.rb并更改each_run阻止,以便在加载时每次加载您需要的测试要求,而不仅仅是加载一次。

答案 2 :(得分:1)

如果您使用spork rc 1.0,则需要使用'spork-rails'。

修复了控制器没有为我重新加载的问题。感谢这篇文章。

http://blog.firsthand.ca/2012/02/heads-up-use-spork-rails-if-you-want-to.html

答案 3 :(得分:0)

我正在阅读相同的教程并遇到了同样的问题。以下是最终为我工作的内容:

  1. 更新'spec / spec_helper.rb'文件以将'Spork.prefork'部分更改为:

    Spork.prefork do
    
      # Loading more in this block will cause your tests to run faster. However,
      # if you change any configuration or code from libraries loaded here, you'll 
      # need to restart spork for it take effect.
    
      ENV["RAILS_ENV"] ||= 'test'
      require File.expand_path("../../config/environment", __FILE__)
      require 'rspec/rails'
    
      # Requires supporting files with custom matchers and macros, etc, 
      # in ./support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
    
      RSpec.configure do |config|
    
      # == Mock Framework
      #
      # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: #
      # config.mock_with :mocha
      # config.mock_with :flexmock 
      # config.mock_with :rr config.mock_with :rspec
          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, comment the following line or assign false 
      # instead of true.
    
      config.use_transactional_fixtures = true
      end  
    
    end
    
  2. 更新'config / environments / test.rb'文件以更改:

    config.cache_classes = true
    

    要:

    config.cache_classes = false
    
  3. 这是书中没有提到的第二点。我通过交叉引用其他StackOverflow问题来解决这个问题。在我的情况下,在关闭'config.cache_classes'之前,我对控制器所做的任何更改都会使测试套件运行,但它不会考虑编辑。这将导致误报和漏报,具体取决于上次测试运行。在禁用设置后,测试会选择我的编辑并提供有效的通过/失败指示器。


    请注意,如果一个视图(.erb)文件通过了一次测试,那么如果您更改它,它似乎不再被拾取。但是,如果视图失败,spork似乎会继续观察它的变化,直到它通过。

答案 4 :(得分:0)

添加到Sam Peacey的答案,在较新版本的Rails instantiate_observers中已被弃用,删除这一行仍然会产生所需的结果:

Spork.each_run do
  ActiveSupport::Dependencies.clear

  load "#{Rails.root}/config/routes.rb"
  Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }  
end if Spork.using_spork?

答案 5 :(得分:-2)

在env.rb(您设置spork的文件)中,您有两个块preforkeach_run。在这里你可以解释他们的所作所为:

Spork.prefork do
      # Loading more in this block will cause your tests to run faster. However, 
      # if you change any configuration or code from libraries loaded here, you'll
      # need to restart spork for it take effect.
end

Spork.each_run do
    # This code will be run each time you run your specs.
end

我认为这是自我描述的;)