Rspec Load Error in Ruby - ruby-on-rails

I am working through Hartl's Ruby on Rails tutorial and am trying to test for user signups with invalid information and am getting a "Load Error" when running rspec. I am unsure how to fix this error, as I have updated my gem files.
$ bundle exec rspec spec/requests/user_pages_spec.rb \ > -e "signup with invalid information"
then I get this message:
/Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load': cannot load such file -- /Users/kelvinyu/rails_projects/sample_app/signup with invalid information (LoadError)
from /Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:77:in `rescue in run'
from /Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:73:in `run'
from /Users/kelvinyu/.rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
Here is my spec/requests/user_pages_spec.rb:
require 'spec_helper'
describe "UserPages" do
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
describe "signup page" do
before { visit signup_path }
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
end
end
And my Gemfile:
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
group :development, :test do
gem 'sqlite3', '1.3.7'
gem 'rspec-rails', '2.13.1'
# The following optional lines are part of the advanced setup.
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', github: 'sporkrb/spork-rails'
gem 'guard-spork', '1.5.0'
gem 'childprocess', '0.3.6'
end
group :test do
gem 'selenium-webdriver', '2.0.0'
gem 'capybara', '2.1.0'
gem 'factory_girl_rails', '4.2.1'
gem 'cucumber-rails', '1.3.0', :require => false
gem 'database_cleaner', github: 'bmabey/database_cleaner'
# Uncomment this line on OS X.
# gem 'growl', '1.0.3'
# Uncomment these lines on Linux.
# gem 'libnotify', '0.8.0'
# Uncomment these lines on Windows.
# gem 'rb-notifu', '0.0.4'
# gem 'win32console', '1.3.2'
end
gem 'sass-rails', '4.0.0'
gem 'uglifier', '2.1.1'
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 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
Am I missing any other information? What is the appropriate step to fix this error?
EDIT:
Spec helper here:
require 'spec_helper'
# Specs in this file have access to a helper object that includes
# the StaticPagesHelper. For example:
# describe StaticPagesHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
#describe StaticPagesHelper do
# pending "add some examples to (or delete) #{__FILE__}"
#end
I notice that it is empty, however, the tutorial's steps did not require any changes.

Your error says the framework cannot load the file "/Users/kelvinyu/rails_projects/sample_app/signup with invalid information" which clearly isn't a file—the file is "spec/requests/user_pages_spec.rb". Try having everything in one line—without the \ > (this slash angle bracket only means that there, maybe, was a line break when Michael Hartl was typing up the tutorial.
Also, look to using describe and context interchangeably. This would make you write clearer specs. There is no magic to it. The sourcecode for RSpec shows that context is just another name for describe. But when you write specs with both, the meanings are clearer.

Related

uninitialized constant TestFactories (NameError)

I am a beginner in Ruby on Rails and I am writing a "User sign in" spec for a wiki project, and I am getting the following error:
uninitialized constant TestFactories (NameError)
This is my sign_in_spec.rb
require 'rails_helper'
describe "Sign in flow" do
include TestFactories
before do
#user = authenticated_user
end
describe "successful" do
it "redirects user to the wikis index" do
user = authenticated_user
visit root_path
end
end
end
This is my test_factories.rb file:
module TestFactories
def authenticated_user(options={})
user_options = { email: "email#{rand}#fake.com", password: 'password' }.merge(options)
user = User.new(user_options)
user.skip_confirmation!
user.save
user
end
end
This is my Gemfile:
source 'https://rubygems.org'
gem 'rails'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'bootstrap-sass', '~> 3.2.0'
gem 'autoprefixer-rails'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# Testing
group :develpment, :test do
gem 'rspec-rails'
gem 'capybara'
gem 'database_cleaner'
gem 'factory_girl_rails', '~> 4.0'
gem 'pry-rails'
end
# Databases
# Developemnt
gem 'sqlite3'
or you can do something like this in sepc helper
RSpec.configure do |config|
config.include TestFactories
end

Im getting error : No DRb server is running. Running in local process instead

I'm following the ROR tutorials , Im testing with rspec spec/requests/static_pages_spec.rb , Error occurred " No DRb server is running. Running in local process instead"' I did some research , they said it's because Spork server is not running , so i added Spork.each_run do to the spc file, it dosen't help , I also modified the gem file from gem 'guard-spork' to gem 'guard-spork', :github => 'guard/guard-spork' Still the same, Anyone can help? Thanks in Advance!
Spec file:
require 'spec_helper'
Spork.each_run do
end
describe "Static pages" do
let(:base_title) { "Ruby on Rails Tutorial Sample App" }
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
it "should have the base title" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App")
end
it "should not have a custom page title" do
visit '/static_pages/home'
expect(page).not_to have_title('| Home')
end
describe "Contact page" do
it "should have the content 'Contact'" do
visit '/static_pages/contact'
expect(page).to have_content('Contact')
end
it "should have the title 'Contact'" do
visit '/static_pages/contact'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
end
end
end
end
Gemfile :
source 'https://rubygems.org'
ruby '2.0.0'
gem 'rails', '4.0.2'
gem 'bootstrap-sass', '2.3.2.0'
gem 'pg', '0.15.1'
group :development, :test do
gem 'guard-spork', :github => 'guard/guard-spork'
gem 'sprockets', '2.11.0'
gem 'rspec-rails', '2.13.1'
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', '4.0.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.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
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', group: :production
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
You should run spork in different process with command spork. Also, remove Spork part from test and modify you spec_helper file with something like this (move all in Spork.prefork block)
The other thing is that Spork had been replaced (not directly) by spring that does the same thing, but without additional configuration.

Rails: cannot load such file - spec.rb

I'm working through chapter 9 of Hartl's rails tutorial. I just added some tests for the 'edit' page, but it won't run on the command line.
I'm trying to run 'bundle exec rspec spec/requests/user_pages_spec.rb -e "edit page"'
I think there may be a problem with my user_pages_spec.rb . I might have too many 'end' at the bottom.
C:\Sites\sample_app>bundle exec rspec spec/requests/user_page_spec.rb -e "edit p
age"
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rspec-core-2.13.1/lib/rspec
/core/configuration.rb:819:in `load': cannot load such file -- C:/Sites/sample_a
pp/spec/requests/user_page_spec.rb (LoadError)
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rspec-core-2.1
3.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rspec-core-2.1
3.1/lib/rspec/core/configuration.rb:819:in `each'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rspec-core-2.1
3.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rspec-core-2.1
3.1/lib/rspec/core/command_line.rb:22:in `run'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rspec-core-2.1
3.1/lib/rspec/core/runner.rb:80:in `run'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rspec-core-2.1
3.1/lib/rspec/core/runner.rb:17:in `block in autorun'
user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by(email: 'user#example.com') }
it { should have_link('Sign out') }
it { should have_title(user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
end
describe "edit" do
let(:user) { FactoryGirl.create(:user) }
before { visit edit_user_path(user) }
describe "page" do
it { should have_content("Update your profile") }
it { should have_title("Edit user") }
it { should have_link('change', href: 'http://gravatar.com/emails') }
end
describe "with invalid information" do
before { click_button "Save changes" }
it { should have_content('error') }
end
end
end
end
end
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'
gem 'bcrypt-ruby', '3.1.2'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
# The following optional lines are part of the advanced setup.
# 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'
gem 'factory_girl_rails', '4.2.0'
gem 'cucumber-rails', '1.4.0', :require => false
gem 'database_cleaner', github: 'bmabey/database_cleaner'
# Uncomment this line on OS X.
# gem 'growl', '1.0.3'
# Uncomment these lines on Linux.
# gem 'libnotify', '0.8.0'
# Uncomment these lines on Windows.
gem 'rb-notifu', '0.0.4'
gem 'wdm', '0.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.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
I can see that you're executing the wrong file:
Change this:
bundle exec rspec spec/requests/user_page_spec.rb -e "edit p
age"
To:
bundle exec rspec spec/requests/user_pages_spec.rb -e "edit p
age"
It should be
bundle exec rspec spec/requests/user_pages_spec.rb
as your file is user_pages_spec.rb

Ruby on Rails 4 and RSPEC

I am trying to get to grips with Rails and Rspec for testing using Capybara. I’m currently trying to test for uniqueness of a Profile Name for User. However from reading around fixtures should not be used with Rspec. How do I go about adding some base data for me to test against.
Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.3'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
gem 'devise', '~> 3.2.3'
gem 'simple_form', '~> 3.0.1'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
group :development do
gem 'awesome_print'
end
group :development, :test do
gem 'rspec-rails', '~> 2.0'
end
group :test do
gem 'capybara', '~> 2.0'
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
Create_spec.rb
require 'spec_helper'
describe "Creating a new user" do
def create_user(options={})
options[:first_name] ||= "Adam"
options[:last_name] ||= "Sackfield"
options[:profile_name] ||= "Sacki"
options[:email] ||= "email#email.com"
options[:password] ||= "password"
visit "/users/sign_up"
expect(page).to have_content "Sign up"
fill_in "First name", with: options[:first_name]
fill_in "Last name", with: options[:last_name]
fill_in "Profile name", with: options[:profile_name]
fill_in "Email", with: options[:email]
fill_in "Password", with: options[:password]
fill_in "Password confirmation", with: options[:password]
click_button "Sign up"
end
it "a user can register" do
create_user
expect(page).to have_content "You have signed up"
end
it "a user must enter a first name" do
create_user first_name: ""
expect(page).to_not have_content "You have signed up"
end
it "a user must enter a last name" do
create_user last_name: ""
expect(page).to_not have_content "You have signed up"
end
end
Thanks
Use factory_girl_rails gem.
factory_girl_rails provides Rails integration for factory_girl
which is a fixtures replacement with a straightforward definition
syntax, support for multiple build strategies (saved instances,
unsaved instances, attribute hashes, and stubbed objects), and support
for multiple factories for the same class (user, admin_user, and so
on), including factory inheritance.
Create a folder under spec directory as factories.
In spec_helper.rb,
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods ## Add only this line
end
Create factory files under spec/factories directory and define the required factories
as shown in Factory Girl Documentation
In your spec files, use the defined factories as shown here

FactoryGirl deletes all users in database

In Rails Tutorial 2nd Edition by Hartl
When running rspec tests the pagination block in user_pages_spec.rb is deleting all users in the development database instead of just the users created by FactoryGirl. Of course this causes other test to fail now that there aren't anymore users in the database.
user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "index" do
let(:user) { FactoryGirl.create(:user) }
before(:each) do
sign_in user
visit users_path
end
it { should have_title('All users') }
it { should have_content('All users') }
describe "pagination" do
before(:all) { 30.times { FactoryGirl.create(:user) } }
after(:all) { User.delete_all }
it { should have_selector('div.pagination') }
it "should list each user" do
User.paginate(page: 1).each do |user|
expect(page).to have_selector('li', text: user.name)
end
...
Gemfile
source 'https://rubygems.org'
ruby '1.9.3'
gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
gem 'pg', '0.15.1'
gem 'activeresource', '4.0.0'
group :development, :test do
gem 'rspec-rails', '2.13.1'
gem 'annotate', '~> 2.5.0'
end
group :test do
gem 'selenium-webdriver', '2.0.0'
gem 'capybara', '2.1.0'
gem 'factory_girl_rails', '4.2.0'
end
gem 'sass-rails', '4.0.0'
gem 'uglifier', '2.1.1'
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 'pg', '0.15.1'
gem 'rails_12factor'
end
You can try this.
30.times do
User.last.destroy
end
In place of
User.delete_all
This will delete all the 30 users created by FactoryGirl. But will work only if you are not creating more users in between.
Update
The above code was just a hack.
If while testing application is hitting development database, then config/database.yml file need to be checked to ensure that it's not pointing to same db for test and development.
You can also use something like Database Cleaner. Then you would not need to delete the users manually but all the changes to the database would be automatically reverted (when using the transaction strategy).

Resources