Hartl Rails Tutorial - ruby-on-rails

I ran into some trouble with section 6.2.1 (validity test) of Michael Hartl's "Rails Tutorial" and realized that I don't even have a test directory created as a result of
$ rails generate model User name:string email:string
While the tutorial says that I should see the output
invoke active_record
create db/migrate/20140724010738_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
all I am actually seeing is:
invoke active_record
identical db/migrate/20141020202519_create_users.rb
identical app/models/user.rb
invoke rspec
identical spec/models/user_spec.rb
I've researched tons of different sites looking for the answer and noticed people were suggesting to move rspec-rails around in the Gemfile but from what I can see, mine is placed properly. Here is my Gemfile:
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.8'
gem 'bootstrap-sass', '2.3.2.0'
gem 'sprockets', '2.11.0'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', '4.0.0'
gem 'guard-spork', '1.5.0'
gem 'childprocess', '0.3.6'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
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 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
I would really appreciate any help! Thanks

What is happening here is rspec-rails is overriding the default behavior of rails.
Rails by default ships with test_unit (as shown by the invoke test_unit in original output).
Instead with rspec, you generate spec files instead. Thus you have invoke rspec and spec/models/user_spec.rb.
If you remove rspec-rails from your Gemfile and run the bundle install and then rails g model User name:string email:string again, it will generate the test files according to the tutorial.

If you want the same output, put the related rspec gem in the test group only.
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'spork-rails', '4.0.0'
gem 'guard-spork', '1.5.0'
gem 'childprocess', '0.3.6'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
gem 'rspec-rails', '2.13.1'
gem 'guard-rspec', '2.5.0'
end
And the update:
bundle update
It worked for me for the inverse process (invoke rspec on generating the model) placing rspec gems into development and test groups.
If you don't want to use rspec as the test suite, just remove the gems from your gemfile.

It looks like the online version of the Rails Tutorial has recently undergone a major overhaul. Previously, the tutorial used rspec for testing; now it's using the test framework that comes with rails. If you look at section 3.1, the Gemfile for the sample app now looks like this:
source 'https://rubygems.org'
gem 'rails', '4.2.0.beta2'
gem 'sass-rails', '5.0.0.beta1'
gem 'uglifier', '2.5.3'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '4.0.0.beta2'
gem 'turbolinks', '2.3.0'
gem 'jbuilder', '2.2.3'
gem 'rails-html-sanitizer', '1.0.1'
gem 'sdoc', '0.4.0', group: :doc
group :development, :test do
gem 'sqlite3', '1.3.9'
gem 'byebug', '3.4.0'
gem 'web-console', '2.0.0.beta3'
gem 'spring', '1.1.3'
end
group :test do
gem 'minitest-reporters', '1.0.5'
gem 'mini_backtrace', '0.1.3'
gem 'guard-minitest', '2.3.1'
end
group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
end
And in section 3.3.1, the first test looks like this:
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get :home
assert_response :success
end
test "should get help" do
get :help
assert_response :success
end
end
Because the tutorial is now using a different test framework, you will no longer get the same output as the tutorial. And in fact, you will no longer be able to follow along with the tests in the tutorial. Unfortunately, I think you will have to start over. I was on chapter 7, so I am in the same position as you. I emailed the author to see if he would be willing to put up the old version somewhere.
Michael Hartl got back to me. Here's a link to the old version:
http://rails-4-0.railstutorial.org/book

Related

Could not find generator rspec:install. Rails4.0.5 ruby2.0.0

I cant install rspec.
command:
rails generate rspec:install
↓
result:
Could not find generator rspec:install.
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.5'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
gem 'sass-rails', '4.0.2'
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
This is my Gemfile.
And, this tutorial is this one.
http://www.railstutorial.org/
I replicated your gemfile and ran the command. It works for me just so long as the bundle installation finished correctly.
To make sure yours is installing correctly, you might try adding this line to your gemfile so that it can pull all the correct versions.
source 'https://rubygems.org'
Also, you may need to run your bundle install as the following if the postgres implementation gems give you trouble as they sometimes do. The tutorial you're working on uploads to heroku and doesn't require those gems on your development environment:
bundle install --without production
After everything has installed successfully, "rails generate rspec:install" should work.

Rspec error: invalid option --warnings

I am working my way through Michael Hartl's Ruby on Rails Tutorial, and have come to section 3.2.1, where he first introduces test driven development and the use of Rspec. I believe I am faithfully following the tutorial (including editing the spec_helper.rb and static_pages_spec.rb files as directed). I get to the point where I am directed to run:
$ bundle exec spec spec/requests/static_pages_spec.rb
And I get the following message in response:
invalid option --warnings
please use --help for a listing of valid options
Admittedly, I am pretty new to all of this, but I have not been able to find an answer as to what this particular error message means (and what I am supposed to do).
My Gemfile:
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.5'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.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.4.0', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
I am also doing Michael Hartl's Ruby on Rails Tutorial and had the exact same problem. Neither editing the .rspec file nor doing the advanced setup solved the problem. I eventually realized that the specs would run properly if I added "--options PATH" to the command:
bundle exec rspec --options PATH spec/requests/static_pages_spec.rb
That's a pretty old version of rspec. I'd suggest getting something newer (or you could edit your .rspec file to remove --warnings).

Gemfile includes incorrect environment

When I do $ bundle install, it attempts to get gems only meant for production, despite me being on development.
I double checked that I'm on development: echo $RAILS_ENV # outputs "development"
Here is a copy of my gem file. If I remove :staging in the last block, then my bundle installs correctly and ignores this group. Why would adding :staging cause my app to include these gems?
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.0'
gem 'bootstrap-sass', '~> 3.0.1.0.rc'
gem 'bcrypt-ruby', '3.0.1'
# generates names, email addresses, and other placeholders for factories.
gem 'faker'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
gem 'aws-sdk', '1.11.1'
gem 'd3-rails', '~>3.3.7'
# used for file ajax uploads
gem 'remotipart', '~> 1.2'
# used for making server side variables accessible in JS
gem "iconv", "~> 1.0.4"
gem "roo", "~> 1.13.2"
gem 'underscore-rails'
gem 'gon', '4.1.0'
# gem "introjs-rails"
# High voltage for static pages
gem 'high_voltage', '~> 2.0.0'
gem "koala", "~> 1.8.0rc1"
gem 'acts_as_list'
gem "bugsnag"
gem 'devise'
# EC2 server is passenger
gem 'passenger'
# easily create seeds
gem 'seed_dump'
#add crossfilter for exploring high dimensional datasets and dcjs for charts
gem 'crossfilter-rails'
# gem 'dcjs-rails'
gem 'activeadmin', github: 'gregbell/active_admin'
# Monitor site speed.
gem 'newrelic_rpm'
group :development, :test do
gem 'sqlite3', '1.3.8'
# rspec-rails includes RSpec itself in a wrapper to make it play nicely with Rails.
gem 'rspec-rails'
# replaces Rails' default fixtures for feeding test data to the test suite with much more preferable factories
gem 'factory_girl_rails'
# watches your application and tests and runs specs for you automatically when it detects changes.
gem 'guard-rspec'
end
group :test do
# runs a browser
gem 'selenium-webdriver'
# makes it easy to programatically simulate your users' interactions with your application
gem 'capybara'
# Shows growl notifications (os x only)
gem 'growl'
# opens your default web browser upon failed integration specs to show you what your application is rendering.
gem 'launchy' # tims tutorial
# helps clear out db after using selenium in tests
gem 'database_cleaner' # tims tutorial
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
# fuck turbolinks.
# gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
# added for resizing panes on d3fiddle pages
gem 'jquery-ui-rails'
# added for code highlighting on d3fiddle pages
gem 'codemirror-rails'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production, :staging do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
Use bundle install --without staging production.You can find more information about bundle here.
Update:
It is because of Bundler.require(:default, Rails.env) present in config/application.rb that bundle does not install gems present in test and production by default.

Rails calling API causes uninitialized constant error in Heroku

I am using the RMeetup gem to pull data from meetup.com. In development mode, I can feature this code in my controller, model or helper with no issues:
RMeetup::Client.api_key = "matt's key"
RMeetup::Client.fetch(:groups, :lat => #user.latitude, :lon => #user.longitude")
But when I try to deploy, Heroku tells me:
app[web.1]: NameError (uninitialized constant RMeetup::Client):
I'm a novice on Rails, and I don't know how to fix this problem. I've tried creating a new RMeetup model to maybe better house this information and fix the issue, but Rails tells me:
The name 'RMeetup' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.
The RMeetup gem is not located exclusively in the dev section of my gemfile. Complicating the matter, I don't actually save any of the data I receive from Meetup.com until a user fills out a form. So I don't have a natural place to build a model off the results from RMeetup fetch operations. I currently do it in a helper or controller.
So why does my local dev have no problem with "RMeetup::Client" but Heroku does, and how can I fix it?
As requested, here is also the Gemfile:
source 'https://rubygems.org'
ruby '2.0.0'
gem 'rails', '4.0.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'pg', '0.15.1'
gem 'geocoder'
gem 'rmagick'
gem 'carrierwave'
gem 'rMeetup'
group :development, :test do
gem 'rspec-rails', '2.13.1'
gem 'puma'
end
group :test do
gem 'selenium-webdriver', '2.0.0'
gem 'capybara', '2.1.0'
end
gem 'sass-rails', '4.0.0'
gem 'uglifier'
gem 'coffee-rails', '4.0.0'
gem 'jquery-rails', '2.2.1'
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
Try
gem 'rMeetup', require: 'rmeetup'
in your Gemfile

Updated Rails test suite set up?

Anyone know of a more up-to-date version of a Rails Rspec/Guard/Spork/Growl test suite set up?
These used to be great, but have become outdated as Ruby, Rails and the gems upgraded.
http://ygamretuta.me/2011/08/10/rails-3-setting-up-guard-with-rspec-and-spork-with-growl-notifications-in-osx/
https://eq8scrapbook.heroku.com/equivalents_scrap/on_rspec_spork_guard_configuration
Even the M. Hartl Ruby on Rails Tutorial instructions results in Guard tossing up a ChildProcess error and doesn't load the DRb server.
Here's what I've found to fix my problem above:
rails new app_name --skip-test-framework
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.12'
gem 'bootstrap-sass', '2.1'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.0.1'
gem 'will_paginate', '3.0.3'
gem 'bootstrap-will_paginate', '0.0.6'
gem 'jquery-rails', '2.0.2'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
gem 'rspec', '2.11.0'
gem 'guard', '1.6.2'
gem 'guard-rspec', '1.2.1'
gem 'guard-spork', '1.4.2'
gem 'spork-rails', '3.2.1'
gem 'spork', '1.0.0rc3'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.5'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
group :test do
gem 'capybara', '1.1.2'
gem 'factory_girl_rails', '4.1.0'
gem 'database_cleaner', '0.7.0'
gem 'launchy', '2.1.0'
gem 'rb-fsevent', :require => false
gem 'growl', '1.0.3'
end
group :production do
gem 'pg', '0.12.2'
end
Then run this:
bundle update; bundle install; rails g rspec:install; guard init rspec; guard init spork; spork --bootstrap
Guardfile
Put the boostrapped spork block before the rspec block
spec_helper.rb
Put the block starting with "ENV["RAILS_ENV"] ||= 'test'" inside the Spork.prefork block
.rspec
add --drb
Run 'guard' and you should be all set.

Resources