Spork:如何刷新验证和其他代码?

时间:2011-05-02 09:37:59

标签: ruby-on-rails validation rspec spork

我整天都在使用spork,大部分时间它都非常棒。

然而,我经常遇到一些问题,我需要重新启动Spork以便我的测试通过......现在我想知道它是否比它的价值更麻烦。我是ruby的新手,所以有时我无法预测错误是由于刷新问题,还是因为我不熟悉Ruby和Rails而导致错误。

我需要将什么内容放入Spork.each_run块中,以便我的验证和其他内容得到刷新,以便我不必重新启动spork服务器?

由于

4 个答案:

答案 0 :(得分:15)

编辑:如果您可以升级到Ruby 2.0,这是您最好的选择。它足够快,可以让你以常规方式工作,而不需要像Spork,Zeus等工具。从本质上讲,你不需要我在下面写的任何东西。

如果您在开发时仍需要一些减速带,请查看Fast Rails Commands演员。


是的,如果您更改了环境,初始化程序或spec_helper文件(并且保护spork是完美的),您想要重新加载Spork,但是当您更新其中一个类(模型)时则不会,因为这会否定目的像spork这样的工具。我有同样的问题:我可以删除模型中的所有方法,测试仍然会通过,因为Spork在内存中保存“旧”模型类。需要重新启动Spork。

原因:

  

某些插件会导致模型代码被预加载,因此需要进行一些工作来阻止模型代码的发生。

您希望阻止预加载的模型代码,因为如果您进行任何更改(例如验证),则不会“重新加载”它们。

解决方案:

取决于所涉及的宝石。在我的情况下,我不得不处理Devise和FactoryGirl,但实际上,你是按照wiki:https://github.com/sporkrb/spork/wiki/Spork.trap_method-Jujitsu

所述使用Spork.trap_method来实现的。

此外,您可以运行spork -d来获取预加载的文件列表,跟踪可能导致此问题的宝石可能会有所帮助。

实施例: Rails 3.0.x + Rspec2 + Spork 0.9.0.rcX + Capybara + Devise + FactoryGirl

# spec/spec_helper.rb
Spork.prefork do
  # This file is copied to spec/ when you run 'rails generate rspec:install'
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'capybara/rspec'
  require 'capybara/rails'

  # set "gem 'factory_girl', :require => false" in Gemfile
  require 'factory_girl'

  # deal with Devise
  require "rails/application"
  Spork.trap_method(Rails::Application, :reload_routes!)

  require File.dirname(__FILE__) + "/../config/environment.rb"

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

  RSpec.configure do |config|
    config.mock_with :rspec
    config.use_transactional_fixtures = false
    config.before(:suite) do
      DatabaseCleaner.strategy = :transaction
    end
    config.before(:each) do
      DatabaseCleaner.start
    end
    config.after(:each) do
      DatabaseCleaner.clean
    end

    # Devise controller test helpers:
    config.include Devise::TestHelpers, :type => :controller
  end
end

Spork.each_run do
  # deal with factory girl
  Factory.definition_file_paths = [File.join(Rails.root, 'spec', 'factories')]
  Factory.find_definitions
end

请注意,config.cache_classes = true需要在测试环境中设置为true,否则您可能会收到来自FactoryGirl等宝石的错误。

这使我的模型测试(规格)快速运行,并在每次保存文件并激活rspec时“重新加载”它们。

编辑:如果你在Ruby 1.9.3上运行,你可以尝试一个有趣的选择:Zeus - https://github.com/burke/zeus

答案 1 :(得分:14)

使用Guard在您更新课程时重新加载Spork Guard :: Spork允许自动&智能地启动/重新加载RSpec / Cucumber Spork服务器。

  1. https://github.com/guard/guard-spork
  2. http://flux88.com/2011/04/using-guard-spork-with-mongoid-devise/

答案 2 :(得分:3)

来自http://www.rubyinside.com/how-to-rails-3-and-rspec-2-4336.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+RubyInside+%28Ruby+Inside%29

  但是,一个小麻烦仍将存在。如果   你更新app / models / person.rb ,.   更改不会在您的测试中生效   因为Spork还有老人   在记忆中。解决这个问题的一种方法是   编辑config / environments / test.rb和   改变:

config.cache_classes = true
     

要:

config.cache_classes = false

答案 3 :(得分:2)

使用更新版本的Factory Girl,您无需做太多工作。首先,在FactoryGirl.reload中添加Spork.each_run。如果您的工厂具有class参数,则它们应为字符串。

factory :my_model, class: 'MyModel' do...

而不是

factory :my_model, class: MyModel do...