No Growl Notifications with Rails Tutorial 3.2 - ruby-on-rails

Michael Hartl's wonderful Rails Tutorial is now available for Rails 3.2. He continues to outline TDD and BDD using rspec and spork as he did in version 3.0 and adds guard in 3.2. In version 3.0, Hartl includes information about autotest and I was able to get the wonderful growl notifications working. In 3.2, however, he no longer includes autotest or much growl information. Guard is working nicely with spork but there's no notifications. I've ventured out on my own the last couple hours using Hartl's 3.0 and some blog posts but trying to get autotest to work still produces a "LoadError" and a growl notification "could not run tests." Super grateful for any thoughts. I'm on OS X 10.7.3. Here's what I did:
$ gem install autotest -v 4.4.6
$ gem install autotest-rails-pure -v 4.1.2
$ gem install autotest-fsevent -v 0.2.8
$ gem install autotest-growl -v 0.2.16
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.3'
gem 'pg', '0.12.2'
group :development, :test do
gem 'rspec-rails', '2.9.0'
gem 'guard-rspec', '0.5.5'
end
group :assets do
gem 'sass-rails','3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.0'
group :test do
gem 'capybara', '1.1.2'
gem 'rb-fsevent', '0.4.3.1', :require => false
gem 'growl', '1.0.3'
gem 'guard-spork', '0.3.2'
gem 'spork', '0.9.0'
end
~/.autotest
require 'autotest/growl'
require 'autotest/fsevent'
Autotest::Growl::show_modified_files = true
Autotest::Growl::one_notification_per_run = true
Autotest::Growl::clear_terminal = false
Autotest::Growl::hide_label = true
Autotest.add_hook :initialize do |autotest|
autotest.add_mapping(/^spec\/requests\/.*_spec\.rb$/) do
autotest.files_matching(/^spec\/requests\/.*_spec\.rb$/)
end
end
$ autotest
loading autotest/rails
--------------------------------------------------------------------------------
/Users/[me]/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -I.:lib:test -rubygems -e "%w[test/unit spec/requests/static_pages_spec.rb].each { |f| require f }"
/Users/[me]/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- spec_helper (LoadError)
from /Users/[me]/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/[me]/programing/rails/rdale_house/spec/requests/static_pages_spec.rb:1:in `<top (required)>'
from /Users/[me]/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/[me]/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from -e:1:in `block in <main>'
from -e:1:in `each'
from -e:1:in `<main>'
spec/spec_helper.rb
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'
# 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}
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
# 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
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end

I ended up just adapting to Hartl's workflow using Sublime Text 2 as well, however, I did want to make sure that I could switch over to Guard with Growl notifications if I ever wanted to (no need for autotest as far as I know since I believe that's Guard's job), so this is how I did it in my personal environment set up guide. Hope it can be of some assistance to you. I'm on OSX 10.6.8, so there may be some differences you need to do on 10.7.3:
Setup Growl for RSpec test notifications (For Mac):
Growl is likely already installed on the system, but growlnotify probably is not.
Download the Growl-1.2.2.dmg file from here
Open the dmg file and go to Extras > growlnotify > growlnotify.pkg
Follow the wizard to install growlnotify
Configure app for testing (RSpec, Cucumber with Spork and Guard):
$ rails generate rspec:install
$ rails generate cucumber:install
Configure Spork (for RSpec and for Cucumber)
$ spork --bootstrap
$ spork cucumber --bootstrap
Add environment loading
$ subl spec/spec_helper.rb
Move the entire contents under the Instructions into the Spork.prefork block to enable environment loading only once, and also add:
config.mock_with :rspec
Configure Guard:
$ guard init rspec
$ guard init spork
Edit generated default file so Guard doesn’t run all tests after a failing test passes; drb flag is for Spork to run in distributed Ruby.
$ subl Guardfile
guard 'rspec', :version => 2, :all_after_pass => false, :cli => '--drb' do
Config to run test suite in distributed Ruby
$ subl .rspec
--drb
Start Guard with Spork
$ guard
...and you should get Growl notifications.
The related gems I have in my Gemfile are pretty much the same as yours.

The error says you are missing the spec_helper file. Could you try running rails generate rspec:install and the trying again?

I encountered the same issue and went to Guard's GitHub page to see their documentation. The relevant portion is here: https://github.com/guard/guard#growl
It appears that the simple solution is to include the growl gem in the development group, whereas you've only included it in the test group. I did the following to get it working:
Installed Growl Notify as explained in another answer:
Setup Growl for RSpec test notifications (For Mac): Growl is likely
already installed on the system, but growlnotify probably is not.
Download the Growl-1.2.2.dmg file from here
Open the dmg file and go to Extras > growlnotify > growlnotify.pkg
Follow the wizard to install growlnotify
Updated my Gemfile (relevant part only, the rest is matched to the Rails Tutorial):
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
gem 'guard-rspec', '1.2.1'
gem 'guard-spork', '1.2.0'
gem 'childprocess', '0.3.6'
gem 'spork', '0.9.2'
gem 'growl', '1.0.3'
end
Ran in Terminal:
bundle install
Opened a new terminal window and ran:
guard
Growl started working!

Related

Push rejected, failed to compile ruby app

currently i am at chapter 3 of michael hartl's tutorial and i keep running into this problem:
C:/Users/HuiHui/sutdweb/spec/spec_helper.rb:82:in `block in <top (required)>': u
ninitialized constant Capybara (NameError)
This is my Gemfile.rb:
source 'https://rubygems.org'
ruby '1.9.3'
gem 'rails', '4.1.1'
gem 'sqlite3-ruby', '1.3.1', :require => 'sqlite3'
group :development, :test do
gem 'sqlite3'
gem 'rspec-rails'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.3.0'
end
gem 'sass-rails', '4.0.1'
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 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
and this is my spec_helper.rb:
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause this
# file to always be loaded, without a need to explicitly require it in any files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, make a
# separate helper file that requires this one and then use it only in the specs
# that actually need it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
# require 'rspec/rails'
# require 'active_support'
# require 'active_support/core_ext'
require 'rspec/Rails'
require 'capybara/Rails'
RSpec.configure do |config|
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# 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
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
expectations.syntax = :expect
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
mocks.syntax = :expect
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended.
mocks.verify_partial_doubles = true
end
config.include Capybara::DSL
end
rails_helper.rb:
# 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'
# 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 with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
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.maintain_test_schema!
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
when i did git push heroku, they gave me the following error:
Installing rdoc 3.12.2
Installing pg 0.15.1
An error occurred while installing sqlite3-ruby (1.3.1), and Bundler cann
ot
continue.
Make sure that `gem install sqlite3-ruby -v '1.3.1'` succeeds before bund
ling.
!
! Failed to install gems via Bundler.
!
! Detected sqlite3 gem which is not supported on Heroku.
! https://devcenter.heroku.com/articles/sqlite3
!
! Push rejected, failed to compile Ruby app
To git#heroku.com:sutdweb.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git#heroku.com:sutdweb.git'
why is the push rejected? can anyone shed some light on this?
Capybara
In your Gemfile, move capybara out of your test gems :
#Gemfile
gem `capybara`
Heroku will only install the generic and production gems (I.E use the bundle install --without test development command)
This will mean that when you use the require 'Capybara' command without having the gem installed in Heroku, you'll end up with the error you've received.
-
Sqlite
Secondly, you're trying to install the SQLite gem, which is not supported by Heroku.
Get rid of the SQLite gem from your production gem file like this:
#Gemfile
group :development do
gem 'sqlite'
end
This should help your system install the remaining gems to get it working
As mentioned by #cupcake, it seems your Gemfile does have SQLite in the development group - however you are referencing another gem
gem 'sqlite3-ruby', '1.3.1', :require => 'sqlite3' -> this should be placed into your development group too:
#Gemfile
group :development do
gem 'sqlite3-ruby', '1.3.1', :require => 'sqlite3'
end
If you apply the changes to your Gemfile, you should then perform the following steps:
$ git add .
$ git commit -a -m "Heroku"
$ git push heroku master

RSpec 2.13.1 hanging in Ruby 1.9.3 with Rails 3.2.13 and Spork 1.0.0rc3

Trying to resurrect an old project by upgrading gems. Ran into an issue where RSpec hangs on startup with the '--drb' option.
By itself, 'rspec spec' works fine. But start 'spork' in another terminal and then 'rspec --drb spec' spins up CPU to ~40% and just sits.
Using Rails 3.2.13, Ruby 1.9.3-p392, RSpec 2.13.1 and spork-rails 3.2.1, which depends on Spork 1.0.0rc3.
Gemfile
source 'http://rubygems.org'
gem 'rails', '~> 3.2.13'
gem 'devise', '~> 2.2.0'
gem 'event-calendar', :require => 'event_calendar'
gem 'has_scope'
gem 'high_voltage'
gem 'inherited_resources'
gem 'jquery-rails'
gem 'kaminari'
gem 'ransack'
gem 'simple_form'
gem 'validates_timeliness'
# deployment process management
gem 'foreman'
gem 'thin'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '< 2.0'
gem 'bootstrap-sass', '~> 2.3.1'
end
group :production do
gem 'pg'
end
group :development, :test do
gem 'factory_girl_rails', '~> 4.2.0'
gem 'rspec-rails', '~> 2.13.1'
gem 'spork-rails'
gem 'sqlite3'
end
group :test do
gem 'capybara'
gem 'growl'
gem 'launchy'
gem 'ruby-prof'
gem 'test-unit'
end
spec/spec_helper.rb
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
# Prevent Devise from loading the User model super early with it's routes
# see: https://github.com/sporkrb/spork/wiki/Spork.trap_method-Jujitsu
require "rails/application"
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
# 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'
# Since I have included Test::Unit for perfomance tests, disable auto-run
Test::Unit::AutoRunner.need_auto_run = false if defined?(Test::Unit::AutoRunner)
# 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 }
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
# 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"
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
I'm not getting any error messages or feedback on the console, so I don't even know where to start looking for problems. Any suggestions?
Upgrading ruby-build via brew update ruby-build picked up a new version dated 2013-05-01. After the update, I trashed and reinstalled all my Rubies, and the problem seems to have resolved itself.
I don't know what specifically addressed the issue, but here are some notes and a relevant commit: https://github.com/sstephenson/ruby-build/commit/9f8d53365aef52c940095f583cdc82f02caba90f

`require': cannot load such file -- capybara/rspec (LoadError)

I tried to test my project. It was working before and I don't know what i did to whenever i type bundle exec rspec spec/ it says cannot load such file -- capybara/rspec (LoadError). And Please I need an advice to which one is good for testing my MVC in ruby on rails as a newbie.
Gemfile:
------------
group :test do
# Pretty printed test output
gem 'turn', :require => false
gem 'minitest'
gem 'capybara', '1.1.2'
gem 'rb-inotify', '0.8.8'
gem 'libnotify', '0.5.9'
gem 'guard-spork', '0.3.2'
gem 'spork', '0.9.0'
gem 'spork-testunit'
gem 'guard-test'
gem 'ruby-prof'
gem 'factory_girl_rails', '1.4.0'
if RUBY_PLATFORM =~ /linux/
gem 'capybara-webkit'
end
gem 'launchy'
end
group :development, :test do
gem 'rspec-rails', '2.10.0'
gem 'guard-rspec', '0.5.5'
end
spec_helper:
require 'rubygems'
#require 'factory_girl'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
# 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}
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
# 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 = false
# 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
#Capybara.default_driver = :selenium
Capybara.javascript_driver = :webkit
end
Error:
/home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in `require': cannot load such file -- capybara/rspec (LoadError)
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in `block in require'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:236:in `load_dependency'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in `require'
from /home/IN4SYSTEMS/sri.kalai/Documents/promaster/spec/spec_helper.rb:11:in `<top (required)>'
from /home/IN4SYSTEMS/sri.kalai/Documents/promaster/spec/helpers/loc/epcs_helper_spec.rb:1:in `require'
from /home/IN4SYSTEMS/sri.kalai/Documents/promaster/spec/helpers/loc/epcs_helper_spec.rb:1:in `<top (required)>'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `block in load_spec_files'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `map'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load_spec_files'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:22:in `run'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:66:in `rescue in run'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:62:in `run'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:10:in `block in autorun'
Thanks in advance!!!
I hope you did install all required gems? Using bundle install BTW did you change Gemfile, in that case you would be required to install Gems using bundle install.
You could check list of gems available by gem list
Regarding second part of the question, which testing API to use. It is matter of choice. You could stick with rpsec and if you find it falls short of your expectation, then look out for change.
Try gem query --local to see if capybara-screenshot appears in your list of installed gems. (For some reason, this rendered a different list than gem list for me.)
If you don't see it, then try gem install capybara-screenshot. It is not included in the installation of capybara, so seeing capybara in your list of gems alone is not sufficient!
I had the same problem today and I fixed it in the following way:
Comment in file spec_helper.rb
next raws
require 'capybara/rspec'
require 'capybara/rails',
Remove
/usr/local/rvm/gems/ruby-1.9.3-p327#olha/gems/capybara-webkit-1.1.0
from my HDD space.

Using Rspec and Shoulda, why is 'setup' undefined?

UPDATE 2: require {path_to_spec_helper} solves the setup undefined issue, but now all of the static variables are suddenly undefined, and All FactoryGirl-made objects don't pass validation (even though inspecting shows that the object should pass validation). And Changing FactoryGirl to save_with_validation(false) just makes the object nil in my tests, breaking everything.
UPDATE:
I threw this into my code:
context "some context" do
ap self.respond_to?(:setup)
setup do
raise "stop"
And the respond_to line printed true, but then proceeded to throw the method_missing error below. So, I guess it's just undefined within context? It didn't used to be that way.
Original Post:
For some reason, unknown to me, it seems that context / should / setup are undefined in my tests. I'd change all the setup's to before(:each)'s and then there would be a problem with should or context. When I change all of the rspec / shoulda matchers to the old-skool style of describe - before(:each) - it{}, my tests will run, but won't actually get executed. (the progress in the console shows no tests being run (no dots)).
So, I guess, how do I verify my test environment is set up properly?
Here is my configuration
gem file:
# can't be in test group because it's required in the rake file
gem "single_test"# because running all the tests all the time takes too long
group :test do
# helper gems
gem "rspec-rails", "1.3.4"
gem "rspec", "1.3.2"
gem "shoulda"
gem "database_cleaner"
gem "crack" #for XML / JSON conversion
gem "mocha" #required for .requires and .stubs
gem "factory_girl", :require => false
# view and functional
gem "capybara", "1.1.1"
gem "cucumber", "1.1.0"
gem "cucumber-rails", "0.3.2"
gem "culerity"
gem "launchy"
gem "hpricot"
gem "gherkin"
gem "rack"
gem "webrat"
# tools
gem "json"
gem "curb"
end
Required things in test hepler:
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require "bundler/setup"
Bundler.require(:test)
require 'test_help'
require 'spec'
require 'spec/expectations'
require 'factory_girl'
binary info:
ruby 1.8.7
rvm 1.7.2
gem 1.8.21
bundle 1.1.4
rake 0.9.2.2
rails 2.3.14
And my error:
`method_missing': undefined method `setup' for Spec::Example::ExampleGroup::Subclass_1:Class (NoMethodError)
stack trace:
from test/unit/my_test.rb:257
from /Users/me/.rvm/gems/ruby-1.8.7-p352/gems/rspec-1.3.2/lib/spec/example/example_group_methods.rb:188:in `module_eval'
from /Users/me/.rvm/gems/ruby-1.8.7-p352/gems/rspec-1.3.2/lib/spec/example/example_group_methods.rb:188:in `subclass'
from /Users/me/.rvm/gems/ruby-1.8.7-p352/gems/rspec-1.3.2/lib/spec/example/example_group_methods.rb:55:in `describe'
from /Users/me/.rvm/gems/ruby-1.8.7-p352/gems/rspec-1.3.2/lib/spec/example/example_group_factory.rb:31:in `create_example_group'
code around line 257 of the last code bit on the stack:
context "some context" do
setup do
...
If you didn't, first you have to generate your spec/spec_helper.rb and spec/rails_helper.rb files. you can do this with the following command:
rails generate rspec:install.
Once the files are created, make sure rails_helper.rb requires spec_helper.rb, otherwise it will not work and require "rails_helper" on top of your spec files.
You can check more detailed configuration of rails_helper.rb and spec_helper.rb here: https://kolosek.com/rails-rspec-setup

How do I setup rspec with rails 2.3.8 and bundler?

A lot of the guides I've been finding don't use bundler.
this is the part of the gemfile I'm using for tests:
group :test do
gem "cucumber"
gem "cucumber-rails"
gem "launchy"
gem "hpricot"
gem "gherkin"
gem "capybara"
gem "rspec"
gem "rack"
gem "rspec-rails"
gem "webrat"
gem "database_cleaner"
gem "factory_girl"
gem "shoulda", :require => nil
gem "shoulda-matchers", :git => "https://github.com/thoughtbot/shoulda-matchers"
gem "cobravsmongoose"
gem "rcov"
gem "ZenTest"
gem "autotest-growl"
gem "inherited_resources", "1.0.2"
gem "responders", "0.4.2"
end
But even with all that, the generators never exist.
so doing: script/generate rspec
doesn't work, (can't find the rspec) generator
generators would be installed if the gems were installed as plugins... but I think that just adds bloat to the app, and different gems compile differently on different OSes.
So, anyone have any guides for setting up rspec with bundler with rails 2.3.x?
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:
group :test, :development do
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
end
The :require => false options are optional, but it helps the app to start up faster in development if it doesn't have to load testing libraries outside of when SpecHelper.rb requires them.
Install the bundle. Use bundle update for any gems that were already in your Gemfile.
Ensure lib/tasks/rspec.rake and 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}
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.
We still have an app on rails 2.3.8, but we updated it to use bundler (Gemfile), and it has rspec and cucumber working as well.
Make sure you follow the bundler guide to make your app correctly use the Gemfile's gem loading instead of Rails' default: http://gembundler.com/rails23.html
After you get that preinitializer.rb and change the config/boot.rb working correctly, you might need to make sure you're using the right versions of rspec and cucumber.
I think just that generic gem 'rspec-rails' might try installing rspec 2 for you, but that only works on Rails 3 (I believe), so you might need to specifically tell it to use rspec 1.x.
Our test group looks like this (although I think some of these gems may be older than they need to be, it's been awhile since we've updated them since a rails 3 upgrade for the app is pending we're not too worried about what it looks like right now):
group :test, :cucumber do
gem 'autotest-fsevent'
gem 'test-unit', '~>1.2.3'
gem "hoe", "1.5.1"
gem 'autotest-rails', '4.1.0'
gem 'rspec', '1.3.2'
gem 'rspec-rails', '1.3.4'
gem 'cucumber', '0.10.0'#, '0.9.0'
# Change this shinanigans to 0.4.0 when it gets released ;)
gem 'cucumber-rails', '0.3.2'
gem 'database_cleaner', '0.5.2'
gem 'capybara', '0.3.9'
gem 'launchy'
gem 'dupe', '0.5.1'
gem 'factory_girl', '1.2.4'
gem 'email_spec', '~>0.6.2', :require => false
end
After doing this, and running bundle install, I am able to type the command script/generate --help which includes this in the output:
Installed Generators
Rubygems: business_time_config, cucumber, culerity, dupe, email_spec, feature, integration_spec, paperclip, rspec, rspec_controller, rspec_model, rspec_scaffold
Builtin: controller, helper, integration_test, mailer, metal, migration, model, observer, performance_test, plugin, resource, scaffold, session_migration
As you can see, the cucumber and rspec generators are in fact available there.
I think your problem might be the version of rspec it's installing. If it's installing rspec version 2, then that is tied to rails 3, which handles generators in gems differently I believe (I believe they have to be put in a different directory structure). That could be why your rails 2.3.x app isn't seeing them.
You don't have to follow my versions exactly, I'm not a fan (at all) of putting specific versions in a Gemfile but we ended up doing it here way back when because a) we didn't fully understand bundler, and b) We needed to make sure we were getting rails 2.3-compatible gems.
Hopefully this helps! Let me know if you have questions.
The reason the generators don't exist is that when you run rails generate ..., it's executing in the development environment while these gems are only loaded in the test environment.
Option 1
Add them to both development and test environments.
Option 2
Run rails generate ... RAILS_ENV=test
(I'm not positive that this option will work.)
In rails 5.1.4 you there are four easy steps to get your RSpec up and running:
group :development, :test do
gem "database_cleaner"
gem "rspec-rails"
end
Add the above gems to the :test and :development groups in your Gemfile.
run bundle install from the command line
run rails generate rspec:install from the command line, it will create the following files:
create .rspec
create spec
create spec/spec_helper.rb
create spec/rails_helper.rb
configure spec_helper.rb and rails_helper.rb
You can check more detailed info on: https://kolosek.com/rails-rspec-setup.

Resources