在尝试完成Hartl轨道教程的第5章练习3后,我收到以下RSpec错误:
1) Static pages should have the right links on the layout
Failure/Error: click_link "Sign up now!"
Capybara::ElementNotFound:
Unable to find link "Sign up now!"
# ./spec/requests/static_pages_spec.rb:56:in `block (2 levels) in <top (required)>'
似乎无法在按钮上找到立即注册链接。
以下是我的spec / requests / static_pages_spec.rb文件中的相关部分:
it "should have the right links on the layout" do
visit root_path
click_link "About"
expect(page).to have_title(full_title('About Us'))
click_link "Help"
expect(page).to have_title(full_title('Help'))
click_link "Contact"
expect(page).to have_title(full_title('Contact'))
click_link "Sign up now!"
expect(page).to have_title(full_title('Sign Up'))
click_link "Home"
expect(page).to have_content('Sample')
click_link "sample app"
expect(page).to have_content('Sample')
end
询问点击注册链接的页面是否具有页面标题“注册”。但它甚至找不到“立即注册!”链接,如上面的错误所示。这里是app / views / static_pages / home.html.erb的主页,以表明它确实有“立即注册!”链接(作为一个按钮,但我已尝试click_button也没有成功):
<div class="center hero-unit">
<h1>Welcome to the Sample App</h1>
<h2>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</h2>
<%= link_to "Sign up now!", signup_path , class: "btn btn-large btn-primary" %>
</div>
所以我不确定为什么它找不到“立即注册!”当它以这种方式清晰地格式化时链接。当我在本地运行rails服务器时,它显然有该链接。感谢您提供任何指导。
这是注册页面,位于app / views / users / new.html.erb,只是为了覆盖我的基础。正如您所看到的,它确实使页面标题为“注册”:
<% provide(:title, 'Sign Up') %>
<h1>Sign up</h1>
<p>Find me in app/views/users/new.html.erb</p>
最后,这是我的路线,也是我的基地。如您所见,它确实路由到新用户:
SampleApp::Application.routes.draw do
get "users/new"
root 'static_pages#home'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
match '/signup', to: 'users#new', via: 'get'
.
.
.
end
如果它可能是gemfile错误,这是我的gemfile:
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.8'
gem 'bootstrap-sass', '2.3.2.0'
gem 'sprockets', '2.11.0'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
gem 'guard-rspec', '2.5.0'
gem 'spork-rails', '4.0.0'
gem 'guard-spork', '1.5.0'
gem 'childprocess', '0.3.6'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
# Uncomment this line on OS X.
gem 'growl', '1.0.3'
# Uncomment these lines on Linux.
# gem 'libnotify', '0.8.0'
# Uncomment these lines on Windows.
# gem 'rb-notifu', '0.0.4'
# gem 'wdm', '0.1.0'
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
答案 0 :(得分:6)
单击链接后,无头浏览器Capybara会导航到该页面。因此,当您在测试中单击Sign up now!
时,无头浏览器位于“联系”页面,而不是主页。
在您尝试点击&#34之前指定visit root_path
;立即注册!&#34;链接应该工作。
您可以在尝试点击&#34;立即注册之前插入click_link 'Home'
!&#34;。
另一种可能是结构更好的解决方案是对每个链接进行测试:
describe "layout" do
before(:each) do
visit root_path
end
it "should have About link" do
click_link 'About'
expect(page).to have_title(full_title('About Us'))
end
it "should have Help link" do
click_link 'Help'
expect(page).to have_title(full_title('Help'))
end
it "should have Sign Up link" do
click_link "Sign up now!"
expect(page).to have_title(full_title('Sign Up'))
end
(...)
end
祝你好运!