Why do people group rspec under test and development in the Gemfile? - ruby-on-rails

I'm generally clear on bundler Gemfile options, but I'm not sure why rspec (specifically, rspec-rails) should be in both test and development.
Here are my test groupings:
group :development, :test do
gem 'rspec-rails'
gem 'faker'
end
group :test do
gem "factory_girl_rails"
gem "capybara"
gem 'guard-rspec'
gem 'rb-fsevent'
gem 'growl'
end
Does this look ok?

I am quoting the official documentation:
Add rspec-rails to the :test and :development groups in the Gemfile:
group :test, :development do
gem "rspec-rails", "~> 2.6"
end
It needs to be in the :development group to expose generators and rake
tasks without having to type RAILS_ENV=test.

Related

Hartl Rails Tutorial

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

Your Gemfile lists the gem more than once. could cause errors

I am currently getting these messages below when i run bundle install
Your Gemfile lists the gem sqlite3 (= 1.3.5) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
Your Gemfile lists the gem rspec-rails (= 2.10.0) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
Your Gemfile lists the gem rspec-rails (= 2.10.0) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
Your Gemfile lists the gem pg (= 0.12.2) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
I am using Postgresql with Heroku and I believe I am using Postgresql in both my development and testing. I recently migrated/switched from sqlite to postgresql.
Do I still need the sqlite3 gems in :development or in :development, :test ?
And lastly, is there a difference between :development & :development, :test ?
database.yml
development:
adapter: postgresql
encoding: unicode
database: xxxxxx_development
pool: 5
username: xxxxxx
password:
test:
adapter: postgresql
encoding: unicode
database: xxxxxx_development
pool: 5
username: xxxxxx
password:
Gemfile
gem 'rails', '3.2.11'
gem "bootstrap-sass"
gem 'will_paginate'
gem 'bootstrap-will_paginate', '0.0.6'
gem 'pg', '0.12.2'
gem 'pg_search'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.10.0'
gem 'guard-rspec', '0.5.5'
end
group :development do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.10.0'
end
# Test gems on Macintosh OS X
group :test do
gem 'rspec-rails', '2.10.0'
end
group :production do
gem 'pg', '0.12.2'
gem 'rack-google_analytics'
end
You are repeating putting sqlite in the development group here:
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.10.0'
gem 'guard-rspec', '0.5.5'
end
group :development do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.10.0'
end
You can delete your second development group listing here as it adds nothing - you've already put them in development and test in the first statement. Similarly by putting pg outside of any group it's available in all, so adding it to production is a duplicate listing.
A rewrite of your gemfile COULD be:
gem 'rails', '3.2.11'
gem "bootstrap-sass"
gem 'will_paginate'
gem 'bootstrap-will_paginate', '0.0.6'
# gem 'pg', '0.12.2'
gem 'pg_search'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.10.0'
end
group :test do
gem 'guard-rspec', '0.5.5'
end
group :production do
gem 'pg', '0.12.2'
gem 'rack-google_analytics'
end
Also, one friendly tip from someone who's felt the pain: try to use the same db in development as production. I know it can be tricky to get postgres set up locally at first but once it's there it's rock solid and very easy to use. You will risk far fewer bugs due to differences (for example with searching text within fields iirc) between PG and SQLite implementations.
With no sqlite:
gem 'rails', '3.2.11'
gem "bootstrap-sass"
gem 'will_paginate'
gem 'bootstrap-will_paginate', '0.0.6'
gem 'pg', '0.12.2'
gem 'pg_search'
group :development, :test do
gem 'rspec-rails', '2.10.0'
end
group :test do
gem 'guard-rspec', '0.5.5'
end
group :production do
gem 'rack-google_analytics'
end
If you're using pg in all environments than remove the sqlite3 gem.
The difference between :development & :development, :test is that :development, :test is for gems that need to be included in both those environments. :development is for :development only gems.
You have gem sqlite3,pg ,respec-rails more then once in same environment ,you need to remove the duplication of same gem.
Edit your gem file as follows:
gem 'rails', '3.2.11'
gem "bootstrap-sass"
gem 'will_paginate'
gem 'bootstrap-will_paginate', '0.0.6'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.10.0'
gem 'guard-rspec', '0.5.5'
end
group :production do
gem 'pg', '0.12.2'
end

How to allow sass file path in Rails Chrome browser?

I start create new Rails app and can't understand why there is no path to sass files. How to allow sass file path in Rails Chrome browser?
like that
Here is my Gemfile
gem 'bourbon'
gem 'neat'
group :assets do
gem 'coffee-rails'
gem 'sass-rails'
gem 'uglifier'
end
group :development do
gem 'foreman'
gem 'better_errors'
gem 'binding_of_caller'
end
group :development, :test do
gem 'factory_girl_rails'
gem 'rspec-rails', '>= 2.14'
end

heroku run rake > no such file to load -- faker

When running 'heroku run rake' I get this error:
no such file to load -- faker
/app/lib/tasks/sample_data.rake:1:in `require'
/app/lib/tasks/sample_data.rake:1:in `<top (required)>'
I have gem 'faker', '0.3.1' under group :development, :test do in Gemfile.
I have require 'faker' in sample_data.rake
source 'https://rubygems.org'
gem 'rails', '3.2.11'
gem 'gravatar_image_tag', '0.1.0'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.8'
gem 'guard-spork', '1.2.0'
gem 'faker', '0.3.1'
gem 'spork', '0.8.4'
gem 'will_paginate', '3.0'
gem 'webrat', '0.7.1'
gem 'capybara', '1.1.2'
gem 'annotate', '2.5.0'
gem 'factory_girl_rails', '1.0'
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
gem 'jquery-rails', '2.0.2'
group :production do
gem 'pg', '0.12.2'
end
Heroku does not install test or development gems by default.
If you want to load fake in your production, you should remove gem 'faker', '0.3.1' from the group :development, :test do and place it outside any group:
source 'https://rubygems.org'
gem 'rails', '3.2.11'
gem 'gravatar_image_tag', '0.1.0'
gem 'faker', '0.3.1'
group :development, :test do
gem 'sqlite3', '1.3.5'
...
However, if you don't want to load fake, you must ensure that your require fake will be required only when the task is invoked:
task :sample_data => :environment do
require 'faker' #must be inside the task.
...
end
Hope it helps.
EDIT
You can tell Bundler to not load the Gem by:
gem 'faker', '0.3.1', :require => false
I got this issue with the "Rub on Rails 3 Tutorial".
Per gabrielhilals answer, the fix was to move require 'faker' to inside the task
before fix:
require 'faker'
namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
...
...
end
end
after fix:
namespace :db do
require 'faker'
desc "Fill database with sample data"
task :populate => :environment do
...
...
end
end

First steps with rspec, want to test method in /lib/app_name/blah.rb

This is rails 3, with rspec.
How to I create the template for my tests, and where should it be in the /spec folder?
would it be:
/spec/lib/app_name/
I have this in my GEM file:
group :development, :test do
gem 'ruby-debug'
gem 'annotate-models', '1.0.4'
gem 'rspec'
gem 'webrat'
gem 'rspec-rails'
end
What do I need to get my first test going now?
Checkout the documentation at http://relishapp.com/rspec/rspec-rails/v/2-5

Resources