1

我在 Rails 教程第 9 章的最后,这一章让我大吃一惊。我只是为了让这些测试运行(并通过),然后我可以继续第 10 章。任何帮助将不胜感激。

rails_projects/sample_app/spec/requests/authentication_pages_spec.rb:155: syntax error, unexpected $end, expecting keyword_end (SyntaxError)    

是我在尝试运行测试时得到的,我知道这意味着我在某处错过了一个结尾,但是当我在结尾添加一个结尾时,它要求另一个,然后是另一个,然后它说我错过了一个结尾user_pages.spec 然后我在那里添加修复并得到大约 19 个错误。无论如何,这是我的代码。

authentication_pages_spec.rb:

require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do
    before { visit signin_path }

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

 describe "signin" do
  before { visit signin_path }

  describe "with invalid information" do
    before { click_button "Sign in" }

  it { should have_selector('title', text: 'Sign in') }
  it { should have_selector('div.alert.alert-error', text: 'Invalid') }

  describe "after visiting another page" do
    before { click_link "Home" }
    it { should_not have_selector('div.alert.alert-error') }
  end
end

describe "with valid information" do
  let(:user) { FactoryGirl.create(:user) }
  before do
    fill_in "Email", with: user.email
    fill_in "Password", with: user.password
    click_button "Sign in"
  end



  describe "with valid information" do
  let(:user) { FactoryGirl.create(:user) }
  before { sign_in user }

  it { should have_selector('title', text: user.name) }

  it { should have_link('Users',    href: users_path) }
  it { should have_link('Profile',  href: user_path(user)) }
  it { should have_link('Settings', href: edit_user_path(user)) }
  it { should have_link('Sign out', href: signout_path) }

  it { should_not have_link('Sign in', href: signin_path) }

  describe "followed by signout" do
    before { click_link "Sign out" }
    it { should have_link('Sign in') }
    end
   end
  end

describe "authorization" do

 describe "for non-signed-in users" do
  let(:user) { FactoryGirl.create(:user) }

  describe "when attempting to visit a protected page" do
    before do
      visit edit_user_path(user)
      fill_in "Email", with: user.email
      fill_in "Password", with: user.password
      click_button "Sign in"
    end

    describe "after signing in" do

      it "should render the desired protected page" do
        page.should have_selector('title', text: 'Edit user')
      end

      describe "when signing in again" do
        before do
          delete signout_path
          visit signin_path
          fill_in "Email", with: user.email
          fill_in "Password", with: user.password
          click_button "Sign in"
        end

        it "should render the default (profile) page" do
          page.should have_selector('title', text: user.name)
        end
      end
     end
   end

 describe "in the Users controller" do

 describe "visiting the edit page" do
  before { visit edit_user_path(user) }
  it { should have_selector('title', text: 'Sign in') }
end

describe "submitting to the update action" do
  before { put user_path(user) }
  specify { response.should redirect_to(signin_path) }
end

describe "visiting the user index" do
  before { visit users_path }
  it { should have_selector('title', text: 'Sign in') }
  end

 end




describe "as wrong user" do
  let(:user) { FactoryGirl.create(:user) }
  let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
  before { sign_in user }

  describe "visiting Users#edit page" do
    before { visit edit_user_path(wrong_user) }
    it { should_not have_selector('title', text: full_title('Edit user')) }
  end

  describe "submitting a PUT request to the Users#update action" do
    before { put user_path(wrong_user) }
    specify { response.should redirect_to(root_path) }
  end
 end


describe "as non-admin user" do
  let(:user) { FactoryGirl.create(:user) }
  let(:non_admin) { FactoryGirl.create(:user) }

  before { sign_in non_admin }

  describe "submitting a DELETE request to the Users#destroy action" do
    before { delete user_path(user) }
    specify { response.should redirect_to(root_path) }        
    end
    end
  end

  end

这是我的 user_pages_spec.rb:

    require 'spec_helper'

describe "User pages" do

subject { page }

describe "index" do

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

 before(:each) 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
 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 "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: full_title('Sign up')) }
end

describe "signup" do

before { visit signup_path }

describe "with invalid information" do
  it "should not create a user" do
     expect { click_button "Create my account" }.not_to change(User, :count)
  end

describe "error messages" do
before { click_button "Create my account" }

 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

it "should create a user" do
expect do
  click_button "Create my account"
 end.to change(User, :count).by(1)
end

describe "after saving the user" do
 before { click_button "Create my account" }
 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
 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 "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
 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

it "should create a user" do
 expect { click_button submit }.to change(User, :count).by(1)
end
describe "edit" do
 let(:user) { FactoryGirl.create(:user) }
 before do
  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
 end
 end

这是我的错误:

   Failures:

 1) User pages delete links as an admin user should be able to delete another user
    Failure/Error: expect { click_link('delete') }.to change(User, :count).by(-1)
    Capybara::ElementNotFound:
      no link with title, id or text 'delete' found
    # (eval):2:in `click_link'
    # ./spec/requests/user_pages_spec.rb:47:in `block (5 levels) in <top (required)>'
    # ./spec/requests/user_pages_spec.rb:47:in `block (4 levels) in <top (required)>'

  2) User pages delete links as an admin user 
     Failure/Error: it { should have_link('delete', href: user_path(User.first)) }
      expected link "delete" to return something
   # ./spec/requests/user_pages_spec.rb:45:in `block (4 levels) in <top (required)>'

 3) signup page 
   Failure/Error: it { should have_selector('h1', text: 'Sign up') }
   expected css "h1" with text "Sign up" to return something
   # ./spec/requests/user_pages_spec.rb:65:in `block (2 levels) in <top (required)>'

 4) signup page 
   Failure/Error: it { should have_selector('title', text: full_title('Sign up')) }
    expected css "title" with text "Ruby on Rails Tutorial Sample App | Sign up" to return  something
  # ./spec/requests/user_pages_spec.rb:66:in `block (2 levels) in <top (required)>'

5) profile page 
   Failure/Error: it { should have_selector('h1', text: user.name) }
     expected css "h1" with text "Person 38" to return something
  # ./spec/requests/user_pages_spec.rb:58:in `block (2 levels) in <top (required)>'

6) profile page 
 Failure/Error: it { should have_selector('title', text: user.name) }
   expected css "title" with text "Person 39" to return something
 # ./spec/requests/user_pages_spec.rb:59:in `block (2 levels) in <top (required)>'

7) signup page 
 Failure/Error: it { should have_selector('h1', text: 'Sign up') }
   expected css "h1" with text "Sign up" to return something
 # ./spec/requests/user_pages_spec.rb:114:in `block (2 levels) in <top (required)>'

8) signup page 
 Failure/Error: it { should have_selector('title', text: full_title('Sign up')) }
   expected css "title" with text "Ruby on Rails Tutorial Sample App | Sign up" to return something
 # ./spec/requests/user_pages_spec.rb:115:in `block (2 levels) in <top (required)>'

9) signup with valid information edit with valid information 
 Failure/Error: it { should have_selector('title', text: new_name) }
   expected css "title" with text "New Name" to return something
 # ./spec/requests/user_pages_spec.rb:171:in `block (5 levels) in <top (required)>'

10) signup with valid information edit with valid information 
 Failure/Error: it { should have_link('Sign out', href: signout_path) }
   expected link "Sign out" to return something
 # ./spec/requests/user_pages_spec.rb:173:in `block (5 levels) in <top (required)>'

11) signup with valid information edit with valid information 
 Failure/Error: it { should have_selector('div.alert.alert-success') }
   expected css "div.alert.alert-success" to return something
 # ./spec/requests/user_pages_spec.rb:172:in `block (5 levels) in <top (required)>'

12) signup with valid information edit with invalid information 
 Failure/Error: it { should have_content('error') }
   expected there to be content "error" in "with invalid information"
 # ./spec/requests/user_pages_spec.rb:157:in `block (5 levels) in <top (required)>'

13) signup with valid information edit page 
 Failure/Error: it { should have_link('change', href: 'http://gravatar.com/emails') }
   expected link "change" to return something
 # ./spec/requests/user_pages_spec.rb:151:in `block (5 levels) in <top (required)>'

14) signup with valid information edit page 
  Failure/Error: it { should have_selector('title', text: "Edit user") }
   expected css "title" with text "Edit user" to return something
 # ./spec/requests/user_pages_spec.rb:150:in `block (5 levels) in <top (required)>'

15) signup with valid information edit page 
 Failure/Error: it { should have_selector('h1', text: "Update your profile") }
   expected css "h1" with text "Update your profile" to return something
 # ./spec/requests/user_pages_spec.rb:149:in `block (5 levels) in <top (required)>'

16) signup with invalid information error messages 
 Failure/Error: it { should have_selector('title', text: 'Sign up') }
   expected css "title" with text "Sign up" to return something
 # ./spec/requests/user_pages_spec.rb:81:in `block (4 levels) in <top (required)>'

17) signup with valid information after saving the user 
 Failure/Error: it { should have_link('Sign out') }
   expected link "Sign out" to return something
 # ./spec/requests/user_pages_spec.rb:106:in `block (4 levels) in <top (required)>'

18) signup with valid information after saving the user 
 Failure/Error: it { should have_selector('div.alert.alert-success', text: 'Welcome') }
   expected css "div.alert.alert-success" with text "Welcome" to return something
 # ./spec/requests/user_pages_spec.rb:105:in `block (4 levels) in <top (required)>'

19) signup with valid information after saving the user 
 Failure/Error: it { should have_selector('title', text: user.name) }
   expected css "title" with text "Example User" to return something
 # ./spec/requests/user_pages_spec.rb:104:in `block (4 levels) in <top (required)>'

2.39秒完成87个例子,19个失败

失败的例子:

rspec ./spec/requests/user_pages_spec.rb:46 # User pages delete links as an admin user should be able to delete another user
rspec ./spec/requests/user_pages_spec.rb:45 # User pages delete links as an admin user 
rspec ./spec/requests/user_pages_spec.rb:65 # signup page 
rspec ./spec/requests/user_pages_spec.rb:66 # signup page 
rspec ./spec/requests/user_pages_spec.rb:58 # profile page 
rspec ./spec/requests/user_pages_spec.rb:59 # profile page 
rspec ./spec/requests/user_pages_spec.rb:114 # signup page 
rspec ./spec/requests/user_pages_spec.rb:115 # signup page 
rspec ./spec/requests/user_pages_spec.rb:171 # signup with valid information edit with valid information 
rspec ./spec/requests/user_pages_spec.rb:173 # signup with valid information edit with valid information 
rspec ./spec/requests/user_pages_spec.rb:172 # signup with valid information edit with valid information 
rspec ./spec/requests/user_pages_spec.rb:157 # signup with valid information edit with invalid information 
rspec ./spec/requests/user_pages_spec.rb:151 # signup with valid information edit page 
rspec ./spec/requests/user_pages_spec.rb:150 # signup with valid information edit page 
rspec ./spec/requests/user_pages_spec.rb:149 # signup with valid information edit page 
rspec ./spec/requests/user_pages_spec.rb:81 # signup with invalid information error messages 
rspec ./spec/requests/user_pages_spec.rb:106 # signup with valid information after saving the user 
rspec ./spec/requests/user_pages_spec.rb:105 # signup with valid information after saving the user 

rspec ./spec/requests/user_pages_spec.rb:104 # 保存用户后使用有效信息注册

4

2 回答 2

1

对。endU在 authentication_pages_spec 中错过了 2 个。enduser_pages_spec 中还有一个额外的内容。将(或删除)这些末端放在文件的末尾,应该这样做。我建议使用一些好的编辑器进行编码。请参阅https://stackoverflow.com/questions/91445/best-ide-for-ruby-on-rails

于 2012-12-15T17:47:03.663 回答
1

我想到了。在分页块之后我有一个额外的结尾,所以我将它删除并添加到结尾所有测试现在都通过了。那些绿点从来没有这么好看过。

于 2012-12-15T19:15:57.560 回答