Rails Rspec error - undefined method `visit' - ruby-on-rails

So I'm new to TDD & I'm throwing some Rspec errors here on my tests...Basically after running bundle exec rspec spec, I get an undefined method 'visit' error on some of my specs. Any help on how to make these tests pass would be much appreciated: Thanks.
Failures:
1) User pages profile page
Failure/Error: before { visit user_path(user) }
NoMethodError:
undefined method `visit' for # <RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007ffda8049540>
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
2) User pages profile page
Failure/Error: before { visit user_path(user) }
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007ffda4f3ac38>
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
3) User pages signup page
Failure/Error: before { visit signup_path }
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_2:0x007ffda8262e58>
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
4) User pages signup page
Failure/Error: before { visit signup_path }
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_2:0x007ffda82663c8>
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
Finished in 9.08 seconds
24 examples, 4 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:11 # User pages profile page
rspec ./spec/requests/user_pages_spec.rb:12 # User pages profile page
rspec ./spec/requests/user_pages_spec.rb:18 # User pages signup page
rspec ./spec/requests/user_pages_spec.rb:19 # User pages signup page
My spec/requests/user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) } # Code to make a user variable
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: 'Sign up') }
end
end
And my spec/models/user_spec.rb
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
it { should be_valid }
describe "when name is not present" do
before { #user.name = " " }
it { should_not be_valid }
end
describe "when email is not present" do
before { #user.email = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { #user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user#foo,com user_at_foo.org example.user#foo.
foo#bar_baz.com foo#bar+baz.com]
addresses.each do |invalid_address|
#user.email = invalid_address
#user.should_not be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user#foo.COM A_US-ER#f.b.org frst.lst#foo.jp a+b#baz.cn]
addresses.each do |valid_address|
#user.email = valid_address
#user.should be_valid
end
end
end
describe "when email address is already taken" do
before do
user_with_same_email = #user.dup
user_with_same_email.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before { #user.password = #user.password_confirmation = " " }
it { should_not be_valid }
end
describe "with a password that's too short" do
before { #user.password = #user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { #user.save }
let(:found_user) { User.find_by_email(#user.email) }
describe "with valid password" do
it { should == found_user.authenticate(#user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not == user_for_invalid_password }
specify { user_for_invalid_password.should be_false }
end
end
describe "when password doesn't match confirmation" do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "when password confirmation is nil" do
before { #user.password_confirmation = nil }
it { should_not be_valid }
end
end
And lastly my views/users/new.html.erb
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<p>Find me in app/views/users/new.html.erb</p>
and views/users/show.html.erb
<% provide(:title, #user.name) %>
<h1><%= #user.name %></h1>
& I've added my UsersController
class UsersController < ApplicationController
def new
end
def show
#user = User.find(params[:id])
end
end
Also getting this new error now after fixing as per Billy's solution
Failures:
1) User pages signup page
Failure/Error: it { should have_selector('title', text: 'Sign up') }
Capybara::ExpectationNotMet:
expected to find css "title" with text "Sign up" but there were no matches. Also found "", which matched the selector but not all filters.
# ./spec/features/user_pages_spec.rb:19:in `block (3 levels) in <top (required)>'
2) User pages profile page
Failure/Error: it { should have_selector('title', text: user.name) }
Capybara::ExpectationNotMet:
expected to find css "title" with text "Michael Hartl" but there were no matches. Also found "", which matched the selector but not all filters.
# ./spec/features/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'
Finished in 9.41 seconds
24 examples, 2 failures
Failed examples:
rspec ./spec/features/user_pages_spec.rb:19 # User pages signup page
rspec ./spec/features/user_pages_spec.rb:12 # User pages profile page

The problem is here:
./spec/requests/user_pages_spec.rb
You put the Capybara integration tests in requests folder. That's why the method visit won't work.
To fix, just move all the tests from spec/requests to spec/features.

This happens because you are trying to use the visit method from Capybara::DSL.
If you check the documentation:
Capybara is no longer supported in
request specs as of Capybara 2.0.0. The recommended way to use Capybara is
with feature specs.
To solve this problem you should move your tests to spec/features folder or include Capybara::DSL for the request specs:
#spec_helper.rb
RSpec.configure do |config|
#...
config.include Capybara::DSL, :type => :request

Related

User authenticate not passing rspec testing

I am following the Ruby on Rails Tutorial Learn Rails by Example Michael Hartli and am getting this error during rspec testing for authenticating the password. i think the other two errors are because of the first one. so just need a little help understanding whats going wrong and a solution.
Rays-MBP:sample_app raycove$ bundle exec rspec spec/
............F...............FF..
Failures:
1) User return value of authenticate method with valid password
Failure/Error: it { should == found_user.authenticate(#user.password) }
expected: #<User id: 6, name: "Example User", email: "user#example.com", created_at: "2016-02-19 21:45:28", updated_at: "2016-02-19 21:45:28", password_digest: "$2a$04$mweZJiYW0O/iDts7bCwUS.hb9GUznoH3nSGSeWdMHuW...">
got: #<User id: nil, name: "Example User", email: "user#example.com", created_at: nil, updated_at: nil, password_digest: "$2a$04$juZNb6HH/yrXQUhYBd.tCeFqWYnwuQs2x.batWwMWhY..."> (using ==)
Diff:
## -1,9 +1,9 ##
-#<User:0x007fa71611a4f8
- id: 6,
+#<User:0x007fa7161b0f70
+ id: nil,
name: "Example User",
email: "user#example.com",
- created_at: Fri, 19 Feb 2016 21:45:28 UTC +00:00,
- updated_at: Fri, 19 Feb 2016 21:45:28 UTC +00:00,
+ created_at: nil,
+ updated_at: nil,
password_digest:
- "$2a$04$mweZJiYW0O/iDts7bCwUS.hb9GUznoH3nSGSeWdMHuWpxMMAV2W42">
+ "$2a$04$juZNb6HH/yrXQUhYBd.tCeFqWYnwuQs2x.batWwMWhYJQtIAKvTou">
# ./spec/models/user_spec.rb:108:in `block (4 levels) in <top (required)>'
2) User pages profile page
Failure/Error: let(:user) { FactoryGirl.create(:user) }
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
# ./spec/requests/user_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
3) User pages profile page
Failure/Error: let(:user) { FactoryGirl.create(:user) }
ActiveRecord::RecordInvalid:
Validation failed: Email has already been taken
# ./spec/requests/user_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
Finished in 0.34838 seconds
32 examples, 3 failures
Failed examples:
rspec ./spec/models/user_spec.rb:108 # User return value of authenticate method with valid password
rspec ./spec/requests/user_pages_spec.rb:11 # User pages profile page
rspec ./spec/requests/user_pages_spec.rb:12 # User pages profile page
Randomized with seed 13988
My user_pages_spec
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_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: 'Sign up') }
end
end
My user_spec
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
describe "when name is not present" do
before { #user.name = " " }
it { should_not be_valid }
end
describe "when email is not present" do
before { #user.email = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { #user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user#foo,com user_at_foo.org example.user#foo.
foo#bar_baz.com foo#bar+baz.com]
addresses.each do |invalid_address|
#user.email = invalid_address
#user.should_not be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user#foo.COM A_US-ER#f.b.org frst.lst#foo.jp a+b#baz.cn]
addresses.each do |valid_address|
#user.email = valid_address
#user.should be_valid
end
end
end
describe "when email address is already taken" do
before do
user_with_same_email = #user.dup
user_with_same_email.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before { #user.password = #user.password_confirmation = " " }
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "when password confirmation is nil" do
before { #user.password_confirmation = nil }
it { should_not be_valid }
end
describe "with a password that's too short" do
before { #user.password = #user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { #user.save }
let(:found_user) { User.find_by_email(#user.email) }
describe "with valid password" do
it { should == found_user.authenticate(#user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not == user_for_invalid_password }
specify { user_for_invalid_password.should be_false }
end
end
end
My user.rb
class User < ActiveRecord::Base
private
# Using a private method to encapsulate the permissible parameters
# is just a good pattern since you'll be able to reuse the same
# permit list between create and update. Also, you can specialize
# this method with per-user checking of permissible attributes.
def user_params
params.require(:user).permit :name, :email, :password, :password_confirmation
end
has_secure_password
before_save { |user| user.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
end
I belive what is happening is when it runs through and gets to the describe "return value of authenticate method" do section in user_spec
it changed the password or something as the passwords dont match but they are pre set so this cant be, any help is very much appreciated.

Failure/Error:before { visit user_path(user) }

I am currently at chapter 7 of michael hartl's tutorial and i keep getting failing test suites.
When i run bundle exec rspec spec/requests/user_pages_spec.rb i get the following failures. Can anyone shed some light on this??
Failures:
1) User pages profile page
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method `digest' for #<Class:0x007fadc6123b28>
# ./app/helpers/sessions_helper.rb:19:in `current_user'
# ./app/helpers/sessions_helper.rb:11:in `signed_in?'
# ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__873134851071575186_70192169771720'
# ./app/views/layouts/application.html.erb:12:in `_app_views_layouts_application_html_erb__3681928384030501247_70192144555120'
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
2) User pages profile page
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method `digest' for #<Class:0x007fadc6123b28>
# ./app/helpers/sessions_helper.rb:19:in `current_user'
# ./app/helpers/sessions_helper.rb:11:in `signed_in?'
# ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__873134851071575186_70192169771720'
# ./app/views/layouts/application.html.erb:12:in `_app_views_layouts_application_html_erb__3681928384030501247_70192144555120'
# ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
3) User pages signup page
Failure/Error: before { visit signup_path }
ActionView::Template::Error:
First argument in form cannot contain nil or be empty
# ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___2899461793876151964_70192172593300'
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
4) User pages signup page
Failure/Error: before { visit signup_path }
ActionView::Template::Error:
First argument in form cannot contain nil or be empty
# ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb___2899461793876151964_70192172593300'
# ./spec/requests/user_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
The following is my user_spec.rb:
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
it { should be_valid }
describe "when name is not present" do
before { #user.name = " " }
it { should_not be_valid }
end
describe "when email is not present" do
before { #user.email = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { #user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user#foo,com user_at_foo.org example.user#foo.
foo#bar_baz.com foo#bar+baz.com]
addresses.each do |invalid_address|
#user.email = invalid_address
expect(#user).not_to be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user#foo.COM A_US-ER#f.b.org frst.lst#foo.jp a+b#baz.cn]
addresses.each do |valid_address|
#user.email = valid_address
expect(#user).to be_valid
end
end
end
describe "when email address is already taken" do
before do
user_with_same_email = #user.dup
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when email address is already taken" do
before do
user_with_same_email = #user.dup
user_with_same_email.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: " ", password_confirmation: " ")
end
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "with a password that's too short" do
before { #user.password = #user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { #user.save }
let(:found_user) { User.find_by(email: #user.email) }
describe "with valid password" do
it { should eq found_user.authenticate(#user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not eq user_for_invalid_password }
specify { expect(user_for_invalid_password).to be_false }
end
end
end
And this is my 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
end
The following is users_helper.rb:
module UsersHelper
# Returns the Gravatar (http://gravatar.com/) for the given user.
def gravatar_for(user)
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}"
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
end
Can anyone shed some light on this?
While you mentioned that you are currently on Chapter 7 of the book, the tests failures that are occurring appear in Chapter 8. Perhaps you should check whether the state of your code base matches where you currently are in the book.
For example, the reference to User.digest in app/helpers/sessions_helper.rb.
Failures:
1) User pages profile page
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined method digest' for #<Class:0x007fadc6123b28>
# ./app/helpers/sessions_helper.rb:19:incurrent_user'
# ./app/helpers/sessions_helper.rb:11:in signed_in?'
# ./app/views/layouts/_header.html.erb:9:in_app_views_layouts__header_html_erb__873134851071575186_70192169771720'
# ./app/views/layouts/application.html.erb:12:in _app_views_layouts_application_html_erb__3681928384030501247_70192144555120'
# ./spec/requests/user_pages_spec.rb:9:inblock (3 levels) in '

undefined method Hartl chapter 6

I'm currently working on 6.3.3 of Hartl's tutorial http://www.railstutorial.org/book/modeling_users
after running rspec spec/ I'm getting the following failures - I'm sure I'm misunderstanding the instructions or something. I'm grateful for any insight into what I'm doing wrong:
leo$ rspec spec/
............FFF...................
Failures:
1) User return value of authenticate method with invalid password
Failure/Error: let(:user_for_invalid_password) { found_user.authenticate("invalid") }
NoMethodError:
undefined method `authenticate' for nil:NilClass
# ./spec/models/user_spec.rb:92:in `block (4 levels) in <top (required)>'
# ./spec/models/user_spec.rb:95:in `block (4 levels) in <top (required)>'
2) User return value of authenticate method with invalid password
Failure/Error: it { should_not eq user_for_invalid }
NameError:
undefined local variable or method `user_for_invalid' for # <RSpec::Core::ExampleGroup::Nested_2::Nested_10::Nested_2:0x007faed251c830>
# ./spec/models/user_spec.rb:94:in `block (4 levels) in <top (required)>'
3) User return value of authenticate method with valid password
Failure/Error: it { should eq found_user.authenticate(#user.password) }
NoMethodError:
undefined method `authenticate' for nil:NilClass
# ./spec/models/user_spec.rb:88:in `block (4 levels) in <top (required)>'
Finished in 0.24185 seconds
34 examples, 3 failures
Failed examples:
rspec ./spec/models/user_spec.rb:95 # User return value of authenticate method with invalid password
rspec ./spec/models/user_spec.rb:94 # User return value of authenticate method with invalid password
rspec ./spec/models/user_spec.rb:88 # User return value of authenticate method with valid password
here is what I have in my user_spec.rb file:
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
it { should be_valid }
describe "when name is not present" do
before { #user.name = " " }
it { should_not be_valid }
end
describe "when email is not present" do
before { #user.email = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { #user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user#foo,com user_at_foo.org example.user#foo. foo#bar_baz.com foo#bar+baz.com]
addresses.each do |invalid_address|
#user.email = invalid_address
expect(#user).not_to be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user#foo.COM A_US-er#f.b.org frst.1st#foo.jp a+b#baz.cn]
addresses.each do |valid_address|
#user.email = valid_address
expect(#user).to be_valid
end
end
end
describe "when email address is already taken" do
before do
user_with_same_email = #user.dup
user_with_same_email.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before do
#user = User.new(name: "Example User", email: "user#example.com", password: " ", password_confirmation: " ")
end
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "with a password that's too short" do
before { #user.password = #user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { #user.save }
let(:found_user) { User.find_by(email: #user.password) }
describe "with valid password" do
it { should eq found_user.authenticate(#user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not eq user_for_invalid }
specify { expext(user_for_invalid_password).to be_false }
end
end
end
and here's what I have in my user.rb file:
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, length: { minimum:6 }
end
Found your typo:
describe "return value of authenticate method" do
# should find by #user.email
let(:found_user) { User.find_by(email: #user.password) }
end

Ruby on Rails Tutorial Chapter 6 RSpec tests are failing, dont know why

I am new in ROR, I am following ruby on rails tutorial by Hartl. Tests of chapter 6 are failing when I am passing:
$ bundle exec rspec spec/
Following tests are failing:
Failures:
1) Static pages Home page should have the title 'Home'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/home'←[0m
←[31mActionController::RoutingError←[0m:
←[31mNo route matches [GET] "/static_pages/home"←[0m
←[36m # ./spec/requests/static_pages_spec.rb:13:in `block (3 levels) in <top
(required)>'←[0m
2) Static pages Home page should have the content 'Sample App'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/home'←[0m
←[31mActionController::RoutingError←[0m:
←[31mNo route matches [GET] "/static_pages/home"←[0m
←[36m # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top
(required)>'←[0m
3) Static pages Help page should have the title 'Help'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/help'←[0m
←[31mActionController::RoutingError←[0m:
←[31mNo route matches [GET] "/static_pages/help"←[0m
←[36m # ./spec/requests/static_pages_spec.rb:26:in `block (3 levels) in <top
(required)>'←[0m
4) Static pages Help page should have the content 'Help'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/help'←[0m
←[31mActionController::RoutingError←[0m:
←[31mNo route matches [GET] "/static_pages/help"←[0m
←[36m # ./spec/requests/static_pages_spec.rb:21:in `block (3 levels) in <top
(required)>'←[0m
5) Static pages About page should have the title 'About Us'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/about'←[0m
←[31mActionController::RoutingError←[0m:
←[31mNo route matches [GET] "/static_pages/about"←[0m
←[36m # ./spec/requests/static_pages_spec.rb:39:in `block (3 levels) in <top
(required)>'←[0m
6) Static pages About page should have the content 'About Us'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/about'←[0m
←[31mActionController::RoutingError←[0m:
←[31mNo route matches [GET] "/static_pages/about"←[0m
←[36m # ./spec/requests/static_pages_spec.rb:34:in `block (3 levels) in <top
(required)>'←[0m
7) UserPages signup page
←[31mFailure/Error:←[0m ←[31mit { should have_title(full_title('Sign up'))
}←[0m
←[31mNoMethodError←[0m:
←[31mundefined method `full_title' for #<RSpec::Core::ExampleGroup::Neste
d_3::Nested_1:0x4807bc0>←[0m
←[36m # ./spec/requests/user_pages_spec.rb:11:in `block (3 levels) in <top (
required)>'←[0m
8) UserPages signup page
←[31mFailure/Error:←[0m ←[31mit { should have_content('Sign up') }←[0m
←[31mexpected #has_content?("Sign up") to return true, got false←[0m
←[36m # ./spec/requests/user_pages_spec.rb:10:in `block (3 levels) in <top (
required)>'←[0m
Finished in 0.70304 seconds
←[31m27 examples, 8 failures←[0m
Failed examples:
←[31mrspec ./spec/requests/static_pages_spec.rb:12←[0m ←[36m# Static pages Home
page should have the title 'Home'←[0m
←[31mrspec ./spec/requests/static_pages_spec.rb:7←[0m ←[36m# Static pages Home p
age should have the content 'Sample App'←[0m
←[31mrspec ./spec/requests/static_pages_spec.rb:25←[0m ←[36m# Static pages Help
page should have the title 'Help'←[0m
←[31mrspec ./spec/requests/static_pages_spec.rb:20←[0m ←[36m# Static pages Help
page should have the content 'Help'←[0m
←[31mrspec ./spec/requests/static_pages_spec.rb:38←[0m ←[36m# Static pages About
page should have the title 'About Us'←[0m
←[31mrspec ./spec/requests/static_pages_spec.rb:33←[0m ←[36m# Static pages About
page should have the content 'About Us'←[0m
←[31mrspec ./spec/requests/user_pages_spec.rb:11←[0m ←[36m# UserPages signup pag
e ←[0m
←[31mrspec ./spec/requests/user_pages_spec.rb:10←[0m ←[36m# UserPages signup pag
e ←[0m
Randomized with seed 53867
Here is the user model:
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
has_secure_password
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
end
Here is the RSpec model/user_spec.rb:
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
it { should be_valid }
describe "when name is not present" do
before { #user.name = " " }
it { should_not be_valid }
end
describe "when email is not present" do
before { #user.email = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { #user.name = "a" * 51 }
it { should_not be_valid }
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user#foo,com user_at_foo.org example.user#foo.
foo#bar_baz.com foo#bar+baz.com]
addresses.each do |invalid_address|
#user.email = invalid_address
expect(#user).not_to be_valid
end
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user#foo.COM A_US-ER#f.b.org frst.lst#foo.jp a+b#baz.cn]
addresses.each do |valid_address|
#user.email = valid_address
expect(#user).to be_valid
end
end
end
describe "when email address is already taken" do
before do
user_with_same_email = #user.dup
user_with_same_email.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
describe "when password is not present" do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: " ", password_confirmation: " ")
end
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
describe "with a password that's too short" do
before { #user.password = #user.password_confirmation = "a" * 5 }
it { should be_invalid }
end
describe "return value of authenticate method" do
before { #user.save }
let(:found_user) { User.find_by(email: #user.email) }
describe "with valid password" do
it { should eq found_user.authenticate(#user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
it { should_not eq user_for_invalid_password }
specify { expect(user_for_invalid_password).to be_false }
end
end
end
Routes.rb file:
FirstApp::Application.routes.draw do
get "users/new"
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
end
user_pages_spec.rb file:
require 'spec_helper'
describe "UserPages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
end
static_pages_spec.rb file:
require 'spec_helper'
describe "Static pages" do
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 title 'Home'" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home")
end
end
describe "Help page" do
it "should have the content 'Help'" do
visit '/static_pages/help'
expect(page).to have_content('Help')
end
it "should have the title 'Help'" do
visit '/static_pages/help'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About page" do
it "should have the content 'About Us'" do
visit '/static_pages/about'
expect(page).to have_content('About Us')
end
it "should have the title 'About Us'" do
visit '/static_pages/about'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")
end
end
end
You have defined the following routes,
users_new GET /users/new(.:format) users#new
root GET / static_pages#home
signup GET /signup(.:format) users#new
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
In static_pages_spec.rb, you are accessing routes as visit '/static_pages/home', visit '/static_pages/help' and visit '/static_pages/about' which would obviously result in No route matches error as these routes don't exist (match them against the routes listed above).
You need to make following changes in static_pages_spec.rb:
Error: No route matches [GET] "/static_pages/home"
Replace
visit '/static_pages/home'
With
either visit root_path or visit '/'
Error: No route matches [GET] "/static_pages/help"
Replace
visit '/static_pages/help'
With
either visit help_path or visit '/help'
Error: No route matches [GET] "/static_pages/about"
Replace
visit '/static_pages/about'
With
either visit about_path or visit '/about'
You receive next 2 errors in user_pages_spec.rb file:
Error: undefined method 'full_title'
It clearly gives the clue that you are using a method named full_title which is not defined anywhere.
it { should have_title(full_title('Sign up')) }
## ^
## full_title method here is undefined
Error: expected #has_content?("Sign up") to return true, got false
This error simply means that the following example is failing
it { should have_content('Sign up') }
That means sign_up view does not have the text Sign up. Make sure that sign_up view has the text Sign up exactly, match the exact case as well.

Syntax error "syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)"

I can't run my Rspec user_spec.rb test due to a syntax error. Too many "end" perhaps in 2 different files? I've added and deleted 'end' in certain places without success.
Syntax error "syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)"
require 'spec_helper'
user_spec.rb
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: "foobar", password_confirmation: "foobar")
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should be_valid }
before do
#user = User.new(name: "Example User", email: "user#example.com")
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password_digest) }
before do
#user = User.new(name: "Example User", email: "user#example.com")
end
subject { #user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should be_valid }
describe "when name is too long" do
before { #user.name = "a" * 51 }
it { should_not be_valid }
end
end
describe "when email format is invalid" do
it "should be invalid" do
addresses = %w[user#foo,com user_at_foo.org example.user#foo.
foo#bar_baz.com foo#bar+baz.com]
addresses.each do |invalid_address|
#user.email = invalid_address
expect(#user).not_to be_valid
end
end
describe "when email format is valid" do
it "should be valid" do
addresses = %w[user#foo.COM A_US-ER#f.b.org frst.lst#foo.jp a+b#baz.cn]
addresses.each do |valid_address|
#user.email = valid_address
expect(#user).to be_valid
end
end
describe "when email address is already taken" do
before do
user_with_same_email = #user.dup
user_with_same_email.save
end
describe "when password is not present" do
before do
#user = User.new(name: "Example User", email: "user#example.com",
password: " ", password_confirmation: " ")
end
it { should_not be_valid }
end
describe "when password doesn't match confirmation" do
before { #user.password_confirmation = "mismatch" }
it { should_not be_valid }
end
user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
end
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
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
Terminal Output
/usr/local/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': /Users/Abraham/code/sample_app/spec/models/user_spec.rb:85: syntax error, unexpected end-of-input, expecting keyword_end (SyntaxError)
from /usr/local/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 /usr/local/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 /usr/local/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 /usr/local/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 /usr/local/rvm/gems/ruby-2.0.0-p247#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /usr/local/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'
It seems that you have a bunch of describes that never have ends keywords, starting with describe "when email format is invalid" do until describe "when email address is already taken" do
Put an end on those guys and you're probably done =)
Do you perhaps have one too many here?
describe "when name is too long" do
before { #user.name = "a" * 51 }
it { should_not be_valid }
end
end
Just posting in case it help someone else. The cause of this error for me was a missing do after creating a form with form_with. Hope that may help someone else
$ rails server -b $IP -p $PORT - that solved the same problem for me

Resources