使用Spork时,我同时拥有rails_helper和spec_helper?

时间:2014-10-05 21:01:07

标签: ruby-on-rails gem rubygems spork

2014年10月7日上午11点10分更新

原始

_我不确定如何使用rails_helper和spec_helper设置spork。我也在堆栈中使用后卫._

我尝试过不同的组合,但我仍然无法安装它。

Railscasts和Tuts +没有帮助(这一次)

任何人都知道更简单的方法来设置它吗?

:Gemfile

source 'https://rubygems.org'

gem 'coffee-rails', '~> 4.0.0'
gem 'jbuilder', '~> 2.0'
gem 'jquery-rails'
gem 'pg'
gem 'rails', '4.1.6'
gem 'sass-rails', '~> 4.0.3'
gem 'sdoc', '~> 0.4.0',          group: :doc
gem 'simple_form'
gem 'spring',        group: :development
gem 'turbolinks'
gem 'uglifier', '>= 1.3.0'

group :development, :test do
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'capybara'
  gem 'factory_girl_rails'
  gem 'growl'
  gem 'guard-rspec'
  gem 'guard-spork'
  gem 'meta_request'
  gem 'pry-rails'
  gem 'rspec-rails'
  gem 'spork','1.0.0rc0'
  gem 'spork-rails'
  gem 'terminal-notifier-guard'
end

我的rails_helper.rb

ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
  config.infer_spec_type_from_file_location!
end

我的spec_helper.rb

require 'rubygems'
require 'spork'

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

  ActiveRecord::Migration.maintain_test_schema!

  RSpec.configure do |config|
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true
    config.infer_spec_type_from_file_location!
  end

end

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

end

我已经重新设置了这个。现在,负载的发生需要永远。它好像没有加载导轨环境。

思想?

1 个答案:

答案 0 :(得分:2)

我能够得到警卫和spork与rspec3合作。以下是我使用的文件:

my_rails_project / .rspec (由rails generate rspec:install创建):

--color
--require spec_helper

my_rails_project / spec / rails_helper.rb (由rails generate rspec:install创建):

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

# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
#
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending!

RSpec.configure do |config|
  # 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

  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!
end

my_rails_project / spec / spec_helper.rb (由rails generate rspec:install创建):

require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'

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.

  # 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 'rspec/autorun'  #Me: This line produced a deprecation warning in the middle of test output

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

  # Checks for pending migrations before tests are run.
  # If you are not using ActiveRecord, you can remove this line.
  ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

  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

    # 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

    # Run specs in random order to surface order dependencies. If you find an
    # order dependency and want to debug it, you can fix the order by providing
    # the seed, which is printed after each run.
    #     --seed 1234
    config.order = "random"

    #Added by ror_tut
    config.include Capybara::DSL
  end
end

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

end

my_rails_project /的 Guardfile
来源:Ruby on Rails教程(Hartl)根据这些帖子进行了更改:

1)Guard Rspec :cli option is deprecated, change to :cmd option
2)spork 0.9.2 and rspec 3.0.0 = uninitialized constant RSpec::Core::CommandLine (NameError)

# A sample Guardfile
# More info at https://github.com/guard/guard#readme
require 'active_support/inflector'

guard 'rspec', all_after_pass: false, cmd: 'rspec' do
  watch(%r{^spec/.+_spec\.rb$})
  watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
  watch('spec/spec_helper.rb')  { "spec" }

  # Rails example
  watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
  watch(%r{^app/(.*)(\.erb|\.haml)$})                 { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
  watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
  watch(%r{^spec/support/(.+)\.rb$})                  { "spec" }
  watch('config/routes.rb')                           { "spec/routing" }

# Custom Rails Tutorial specs
  watch(%r{^app/controllers/(.+)_(controller)\.rb$}) do |m|
    ["spec/routing/#{m[1]}_routing_spec.rb",
     "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb",
     "spec/acceptance/#{m[1]}_spec.rb",
     (m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" :
                       "spec/requests/#{m[1].singularize}_pages_spec.rb")]
  end
  watch(%r{^app/views/(.+)/}) do |m|
    (m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" :
                      "spec/requests/#{m[1].singularize}_pages_spec.rb")
  end
  watch(%r{^app/controllers/sessions_controller\.rb$}) do |m|
    "spec/requests/authentication_pages_spec.rb"
  end

  #Rails example continued...
  watch('app/controllers/application_controller.rb')  { "spec/controllers" }

  # Capybara features specs
  watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/features/#{m[1]}_spec.rb" }

  # Turnip features and steps
  watch(%r{^spec/acceptance/(.+)\.feature$})
  watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$})   { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end


guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, 
               :rspec_env => { 'RAILS_ENV' => 'test' } do
  watch('config/application.rb')
  watch('config/environment.rb')
  watch('config/environments/test.rb')
  watch(%r{^config/initializers/.+\.rb$})
  watch('Gemfile')
  watch('Gemfile.lock')
  watch('spec/spec_helper.rb') { :rspec }
  watch('test/test_helper.rb') { :test_unit }
  watch(%r{features/support/}) { :cucumber }
end

然后在包含规范的文件中,例如my_rails_projects / spec / requests / static_pages_spec.rb ,使用以下要求:

require 'rails_helper'

请注意,rails_helper.rb包含以下行:

require 'rspec_helper.rb'

...所以你得到require rails_helper的两个文件。

my_rails_project /的的Gemfile

source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=sample_app2_gems

gem 'rails', '4.0.8'
gem 'pg', '0.15.1'source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=sample_app2_gems

gem 'rails', '4.0.8'
gem 'pg', '0.15.1'  #The version in rails tutorial Gemfile, latest is:
#gem 'pg', '0.17.1'

#For Bootstrap css
gem 'bootstrap-sass', '2.3.2.0'
gem 'sprockets', '2.11.0'
gem 'bcrypt-ruby', '3.1.2'

group(:development, :test) do
  #gem 'sqlite3', '1.3.8'
  #gem 'rspec-rails', '2.13.1'
  gem 'rspec-rails', '~>3.0'
  gem 'guard-rspec'
  gem 'spork-rails'
  gem 'guard-spork' 
  gem 'childprocess', '0.3.6'
  gem 'factory_girl_rails', '4.2.0'
end

group :test do
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara'
  gem 'growl', '1.0.3'
end

gem 'sass-rails', '4.0.3'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'rails_12factor', '0.0.2'
end


#gem 'pg', '0.17.1'

#For Bootstrap css
gem 'bootstrap-sass', '2.3.2.0'
gem 'sprockets', '2.11.0'
gem 'bcrypt-ruby', '3.1.2'

group(:development, :test) do
  #gem 'sqlite3', '1.3.8'
  #gem 'rspec-rails', '2.13.1'
  gem 'rspec-rails', '~>3.0'
  gem 'guard-rspec'
  gem 'spork-rails'
  gem 'guard-spork' 
  gem 'childprocess', '0.3.6'
  gem 'factory_girl_rails', '4.2.0'
end

group :test do
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara'
  gem 'growl', '1.0.3'
end

gem 'sass-rails', '4.0.3'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'rails_12factor', '0.0.2'
end

至于第二个链接中提到的问题,即在Guardfile中消除--drb会关闭spork:

在做$ bundle exec guard之前:

$ time bundle exec rspec spec/
No DRb server is running. Running in local process instead ...
.........................................

Finished in 1.24 seconds
41 examples, 0 failures

Randomized with seed 20709


real    0m6.186s
user    0m5.082s
sys     0m1.018s

然后在另一个终端窗口启动守卫:

$ bundle exec guard

然后再次这样做:

$ time bundle exec rspec spec/
.........................................

Finished in 1.66 seconds
41 examples, 0 failures

Randomized with seed 7823


real    0m3.145s
user    0m1.015s
sys     0m0.178s

......你可以看到加速了。