0
votes

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: "[email protected]",
             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[[email protected] [email protected] [email protected] [email protected]]
      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: "[email protected]",
                       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
1
All errors are user_pages_spec.rb and static_pages_spec.rb,, But not in this test code, you provided. Thus give us those 2 files..Arup Rakshit
I have now added all the required files.Mukesh Thawani

1 Answers

2
votes

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:

  1. Error: No route matches [GET] "/static_pages/home"

    Replace

    visit '/static_pages/home'
    

    With

    either visit root_path or visit '/'

  2. Error: No route matches [GET] "/static_pages/help"

    Replace

    visit '/static_pages/help'
    

    With

    either visit help_path or visit '/help'

  3. 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:

  1. 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
    
  2. 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.