Search code examples
ruby-on-rails-3rspecrailstutorial.orgrenderpartial

RoR, RSpec test failing after refactoring


I'm new to programming and I'm having trouble figuring out an expected error in Hartl's Ruby on Rails 3 tutorial. After refactoring some layout pages, I'm getting a recurring error on what seems to be the {visit signup_path}.

Here's the partial refactoring I did:

_fields partial:

<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>

<%= f.label :email %>
<%= f.text_field :email %>

<%= f.label :password %>
<%= f.password_field :password %>

<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>

On new.html.erb:

<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(@user) do |f| %>
      <%= render 'fields', f: f %>
      <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>

On edit.html.erb

<% provide(:title, "Edit user") %> 
<h1>Update your profile</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(@user) do |f| %>
      <%= render 'fields', f: f %>
      <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
    <% end %>

    <%= gravatar_for @user %>
    <a href="http://gravatar.com/emails" target="_blank">change</a>
</div>

And the testing spec that are causing 20 failures:

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "index" do

    let(:user) { FactoryGirl.create(:user) }

    before do
      sign_in user
      visit users_path
    end

    it { should have_selector('title', text: 'All users') }
    it { should have_selector('h1',    text: '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|
          page.should have_selector('li', text: user.name)
        end
      end
    end

    describe "delete links" do

      it { should_not have_link('delete') }

      describe "as an admin user" do
        let(:admin) { FactoryGirl.create(:admin) }
        before do
          sign_in admin
          visit users_path
        end

        it { should have_link('delete', href: user_path(User.first)) }
        it "should be able to delete another user" do
          expect { click_link('delete') }.to change(User, :count).by(-1)
        end
        it { should_not have_link('delete', href: user_path(admin)) }
      end
    end
  end

  describe "signup page" do
    before { visit signup_path }

    it { should have_selector('h1',    text: 'Sign up') }
    it { should have_selector('title', text: full_title('Sign up')) }
  end

  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" 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
      describe "after submission" do
        before { click_button submit }

        it { should have_selector('title', text: 'Sign up') }
        it { should have_content('error') }
      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
      describe "after saving the user" do
        before { click_button submit }
        let(:user) { User.find_by_email('user@example.com') }

        it { should have_selector('title', text: user.name) }
        it { should have_selector('div.alert.alert-success', text: 'Welcome') }
        it { should have_link('Sign out') }
      end

      it "should create a user" do
        expect { click_button submit }.to change(User, :count).by(1)
      end
    end
  end

  describe "edit" do
    let(:user) { FactoryGirl.create(:user) }
    before do
      visit signup_path
      sign_in user
      visit edit_user_path(user)
    end

    describe "page" do
      it { should have_selector('h1',    text: "Update your profile") }
      it { should have_selector('title', text: "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

    describe "with valid information" do
      let(:new_name)  { "New Name" }
      let(:new_email) { "new@example.com" }
      before do
        fill_in "Name",             with: new_name
        fill_in "Email",            with: new_email
        fill_in "Password",         with: user.password
        fill_in "Confirm Password", with: user.password
        click_button "Save changes"
      end

      it { should have_selector('title', text: new_name) }
      it { should have_selector('div.alert.alert-success') }
      it { should have_link('Sign out', href: signout_path) }
      specify { user.reload.name.should  == new_name }
      specify { user.reload.email.should == new_email }
    end
  end
end

And the terminal output after running bundle exec rspec spec/

Failures:

  1) User pages edit with valid information 
     Failure/Error: visit signup_path
     ActionView::Template::Error:
       /Users/ajorczak/rails_projects/sample_app/app/views/users/_fields.html.erb:14: syntax error, unexpected keyword_ensure, expecting $end
     # <internal:prelude>:10:in `synchronize'
     # ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb__3426326572903275614_70133147147220'
     # ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb__3426326572903275614_70133147147220'
     # ./spec/requests/user_pages_spec.rb:111:in `block (3 levels) in <top (required)>'

  2) User pages edit with valid information 
     Failure/Error: visit signup_path
     ActionView::Template::Error:
       /Users/ajorczak/rails_projects/sample_app/app/views/users/_fields.html.erb:14: syntax         error, unexpected keyword_ensure, expecting $end
     # <internal:prelude>:10:in `synchronize'
     # ./app/views/users/new.html.erb:7:in `block in _app_views_users_new_html_erb__3426326572903275614_70133147147220'
     # ./app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb__3426326572903275614_70133147147220'
     # ./spec/requests/user_pages_spec.rb:111:in `block (3 levels) in <top (required)>'

etc. I'm not listing each error because I'm convinced (and Hartl says it's expected) it's the same error in the testing document. Can anyone give me some guidance?

Thank you for your time.


Solution

  • Not sure what I did to fix this, but it seemed to be a number of syntax errors across other areas. Found the solution though; There was a text change from 'Confirmation' to 'Confirm Password'. Thanks for those who took a gander. I'll mark resolved.