Question

Im running rails 2.3.14 on ruby-1.9.2-p180

I'm trying to get guard rspec going.

I installed relevant gems, here are my file contents, for Gemfile, /spec/spec_helper.rb and Guardfile


Gemfile

group :test, :development do
  gem 'rspec-rails', '~> 1.3.2', :require => false
  gem 'simplecov', :require => false
end

group :test do
  gem 'test-unit', '1.2.3', :require => false # for rspec
  gem 'rspec', '~> 1.3.2', '~> 1.2', :require => false
  gem 'guard', :require => false
  gem 'rb-inotify', '~> 0.9', :require => false
  gem 'spork', '~> 0.8', :require => false
  gem 'guard-rspec', :require => false
  gem 'guard-spork', :require => false
  gem 'growl', :require => false      # notifications; optional
  gem 'listen', '>= 0.5.1', :require => false
  gem 'machinist', '~> 2.0', :require => false
  gem 'database_cleaner', '~> 0.9.1', :require => false
end

spec_helper.rb

require 'rubygems'
require 'spork'

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

Spork.each_run do
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
end

Spec::Runner.configure do |config|  
  config.use_transactional_fixtures = true
  config.use_instantiated_fixtures  = false
  config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
end

Guardfile

# A sample Guardfile
# More info at https://github.com/guard/guard#readme

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

guard 'rspec', :version => 1 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" }
  watch('app/controllers/application_controller.rb')  { "spec/controllers" }

  # Capybara request specs
  watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/requests/#{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


When I run

rake spec

it works fine


But when I run

guard

it shows me this error

13:04:24 - INFO - Guard uses NotifySend to send notifications.
13:04:24 - INFO - Guard uses TerminalTitle to send notifications.
13:04:24 - INFO - Starting Spork for RSpec
Using RSpec
Preloading Rails environment
no such file to load -- /home/francois/sites/easylodge/config/application (LoadError)

Update

When I comment this part out in the Guardfile, it looks like it launches fine

# 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

result in console

13:17:01 - INFO - Guard uses NotifySend to send notifications.
13:17:01 - INFO - Guard uses TerminalTitle to send notifications.
13:17:01 - INFO - Guard::RSpec is running, with RSpec 1!
13:17:01 - INFO - Running all specs
13:17:07 - INFO - Guard is now watching at '/home/francois/sites/easylodge'
Was it helpful?

Solution

wrong versions in gemfile :/

new gemfile

group :test, :development do
  gem 'rspec-rails', '~> 1.2', :require => false
  gem 'simplecov', :require => false
end

group :test do
  gem 'test-unit', '1.2.3', :require => false # for rspec
  gem 'rspec', '~> 1.2', :require => false
  gem 'guard', :require => false
  gem 'spork', '~> 0.8.0', :require => false
  gem 'guard-rspec', :require => false
  gem 'guard-spork', :require => false
  gem 'growl', :require => false      # notifications; optional
  gem 'listen', '>= 0.5.1', :require => false
  gem 'machinist', '~> 2.0', :require => false
  gem 'database_cleaner', '~> 0.9.1', :require => false
end


Thanks to this post


Setting up RSpec, Guard, and Spork on a Rails 2 Project

I've done this a few times now; hopefully this will be helpful to anyone needing to maintain Rails 2.3 apps. This has worked great for the apps I've worked on, but I welcome contributions from others who suggest additional steps.

This guide assumes a Rails 2.3.x project on Bundler

Get rid of any old rspec plugins that are in your project, if any. RSpec bits may be hiding in:

  • Rakefile
  • lib/tasks/rspec.rake
  • vendor/plugins/rspec
  • (anything else you can find)


RSpec 2 is not compatible with Rails 2; use RSpec 1 (docs). Put the most recent compatible gem versions to your Gemfile:

gem 'test-unit', '1.2.3', :require => false # for rspec
gem 'rspec', '~> 1.2', :require => false
gem 'rspec-rails', '~> 1.2', :require => false
gem 'guard', :require => false
gem 'spork', '~> 0.8.0', :require => false
gem 'guard-rspec', :require => false
gem 'guard-spork', :require => false
gem 'growl', :require => false      # notifications; optional
gem 'rb-fsevent', :require => false # for OSX; optional
gem 'listen', '>= 0.5.1', :require => false
gem 'machinist', '~> 2.0', :require => false
gem 'database_cleaner', '~> 0.9.1', :require => false

Install the bundle.

Use bundle update for any gems that were already in your Gemfile.

Ensure
lib/tasks/rspec.rake
spec/spec_helper.rb do not exist.

./script/generate rspec



Remove the config.gem line that was added to config/environments/test.rb; the app uses bundler.

spork --bootstrap

Then edit spec/spec_helper.rb and follow the instructions.

Move everything from the stock spec_helper.rb into the prefork block,

except:Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}

which belongs in each_run.

Install database_cleaner. In spec/spec_helper.rb, In the prefork block: require 'database_cleaner' In the each_run block: DatabaseCleaner.clean

Initialize Guardfile

guard init spork

guard init rspec

Modify the Guardfile's rspec guard to use the correct version and drb (spork):

guard 'rspec', :version => 1, :cli => '--drb --color' do


Modify the Guardfile as appropriate for your project Run rake spec. You should get no output (if you have no tests). If you get errors, resolve them.

Run guard. No errors? Great, test away!
Problems? Try again more quickly by running spec spec instead of re-running guard.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top