RoR,RSpec测试在重构后失败

时间:2013-03-06 20:08:31

标签: ruby-on-rails-3 rspec railstutorial.org renderpartial

我是编程新手,我在找出Hartl的Ruby on Rails 3教程中的预期错误时遇到了麻烦。在重构一些布局页面后,我在{visit signup_path}看起来反复出现错误。

这是我做过的部分重构:

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

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

导致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: "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

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

等。我没有列出每个错误,因为我确信(并且Hartl说它是预期的)它在测试文档中是同样的错误。谁能给我一些指导?

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

我不知道我做了什么来解决这个问题,但似乎是其他方面的一些语法错误。找到了解决方案;从“确认”到“确认密码”有文本更改。感谢那些带着雄鹅的人。我会标记已解决。