19

我一整天都在使用 spork,而且大多数时候它真的很棒。

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

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

谢谢

4

4 回答 4

15

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

如果您在开发过程中仍然需要一些减速带,请查看Fast Rails Commands cast。


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

原因:

一些插件会导致模型代码被预加载,因此需要做一些工作来阻止这种情况的发生。

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

解决方案:

取决于所涉及的宝石。就我而言,我必须处理 Devise 和 FactoryGirl,但本质上,您可以使用 wiki 上描述的 Spork.trap_method 来完成:https ://github.com/sporkrb/spork/wiki/Spork.trap_method-Jujitsu

此外,您可以运行spork -d以获取预加载的文件列表,这可能有助于跟踪哪些 gem 可能与导致此问题有关。

示例: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 这样的 gem 中得到错误。

这使我的模型测试(规范)运行得很快,并且每次我保存文件并触发 rspec 时都会“重新加载”它们。

编辑:如果您在 Ruby 1.9.3 上运行,您可以尝试一个有趣的替代方案:Zeus - https://github.com/burke/zeus

于 2011-07-16T07:59:59.070 回答
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/
于 2011-05-02T15:08:20.113 回答
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 的旧 Person 仍在内存中。解决此问题的一种方法是编辑 config/environments/test.rb 并更改:

config.cache_classes = true

至:

config.cache_classes = false
于 2011-05-02T10:17:49.593 回答
2

使用最新版本的 Factory Girl,您无需做太多事情。首先,添加FactoryGirl.reload. Spork.each_run如果你有带class参数的工厂,它们应该是字符串。

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

代替

factory :my_model, class: MyModel do...

于 2013-01-11T00:05:01.127 回答