我不想继续讨论所有人,但我有另一个问题。我想我只是错过了一些非常简单的东西,但我无法找到它会是什么。我在第8章结束时进行了登录/上/下示例应用程序。但是当我运行测试时,我收到以下错误。
Failures:
1) Aithentication signin with valid information followed by signout
Failure/Error: before { click_link "Sign Out" }
Capybara::ElementNotFound:
Unable to find link "Sign Out"
# ./spec/requests/authentication_pages_spec.rb:44:in `block (5 levels) in <top (required)>'
2) User pages signup page after saving the user
Failure/Error: it { should have_selector('div.alert.alert-success', text: 'Welcome') }
expected #has_selector?("div.alert.alert-success", {:text=>"Welcome"}) to return true, got false
# ./spec/requests/user_pages_spec.rb:53:in `block (4 levels) in <top (required)>'
3) User pages signup page after saving the user
Failure/Error: it { should have_title(user.name) }
NoMethodError:
undefined method `name' for nil:NilClass
# ./spec/requests/user_pages_spec.rb:52:in `block (4 levels) in <top (required)>'
4) User pages signup page after saving the user
Failure/Error: it { should have_link('Sign out') }
expected #has_link?("Sign out") to return true, got false
# ./spec/requests/user_pages_spec.rb:51:in `block (4 levels) in <top (required)>'
Finished in 0.66029 seconds
50 examples, 4 failures
Failed examples:
rspec ./spec/requests/authentication_pages_spec.rb:45 # Aithentication signin with valid information followed by signout
rspec ./spec/requests/user_pages_spec.rb:53 # User pages signup page after saving the user
rspec ./spec/requests/user_pages_spec.rb:52 # User pages signup page after saving the user
rspec ./spec/requests/user_pages_spec.rb:51 # User pages signup page after saving the user
这是我的User_pages_spec
require 'spec_helper'
describe "User pages" do
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) } #this was what I added
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
describe "signup page" 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
end
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by(email: 'user@example.com') }
it { should have_link('Sign out') }
it { should have_title(user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
end
end
end
和我的authentication_page_spec
require 'spec_helper'
describe "Aithentication" do
subject { page }
describe "signin page" do
before { visit signin_path }
it { should have_content('Sign in') }
it { should have_title('Sign in') }
end
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in"}
it { should have_title('Sign in') }
it { should have_selector('div.alert.alert-error') }
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.upcase
fill_in "Password", with: user.password
click_button "Sign in"
end
it { should have_title(user.name) }
it { should have_link('Profile', href: user_path(user), visible: false) }
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
end
这是我的_header,其中大部分失败来自我认为
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", '#' %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: "delete" %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>
感谢您的帮助,请告诉我您是否需要其他任何内容。
答案 0 :(得分:1)
capybara通常搜索区分大小写,并搜索带有标签,名称或ID的字段。
在第一个测试案例中
<%= link_to "Sign out", signout_path, method: "delete" %>
describe "followed by signout" do
before { click_link "Sign Out" }
it { should have_link('Sign In') }
end
您在此处搜索的是Sign Out
,其链接中包含Sign out
。
关于第3个测试用例
let(:user) { User.find_by(email: 'user@example.com') }
将此更改为
let(:user) { User.find_by_email('user@example.com') }
希望这能解决您的问题。