我正处于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 #supup并在保存用户后提供有效信息
答案 0 :(得分:1)
右。你在authentication_pages_spec中错过了2 end
。而且user_pages_spec中还有一个end
。将这些末尾放在(或删除)文件的末尾,应该这样做。我建议你使用一些好的编辑器进行编码。请参阅https://stackoverflow.com/questions/91445/best-ide-for-ruby-on-rails。
答案 1 :(得分:1)
我明白了。在分页后我有一个额外的结束所以我删除它并添加到最后所有测试现在通过。那些绿点从未如此美好。