2013-03-06 129 views
0

我是編程新手,在計算Hartl Ruby on Rails 3教程中的預期錯誤時遇到了困難。重構了一些佈局頁面後,我似乎在{visit signup_path}看到一個重複的錯誤。重構後RoR,RSpec測試失敗

這裏的部分重構我所做的:

_fields部分:

<%= 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 %> 

在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> 

在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> 

並且測試規範tha T分別造成20個故障:

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: "[email protected]" 
     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('[email protected]') } 

     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) { "[email protected]" } 
     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 

運行包的exec rspec的規格後,端子輸出/

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)>' 

等,因爲我相信(我不是列出每個錯誤和哈特爾說,這是預計)這是測試文檔中的相同錯誤。任何人都可以給我一些指導嗎?

謝謝你的時間。

回答

1

不知道我做了什麼來解決這個問題,但它似乎是跨越其他領域的一些語法錯誤。雖然找到了解決辦法;文本從「確認」更改爲「確認密碼」。感謝那些耿耿於懷的人。我會標記解決。