我正在关注 Michael Hartl 的 Rails 教程,我正在为您的应用添加一个 Bootstrap 下拉顶部导航栏,该导航栏会根据用户是否登录而改变。
这是使导航栏发生变化的代码
<% 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 %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to 'Sign in', signin_path %></li>
<% end %>
现在,我做了以下 rspec 测试
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
visit '/signin'
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)) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
end
保存并重新加载应用程序后,我一切正常。
但即使应用程序代码本身正在运行,rspec 测试也失败了。
Failures:
1) Authentication with valid information
Failure/Error: it { should have_link('Sign out', href: signout_path) }
expected #has_link?("Sign out", {:href=>"/signout"}) to return true, got
false
# ./spec/requests/authentication_pages_spec.rb:38:in `block (3 levels) in <
top (required)>'
2) Authentication with valid information
Failure/Error: it { should have_link('Profile', href: user_path(user))
}
expected #has_link?("Profile", {:href=>"/users/1"}) to return true, got f
alse
# ./spec/requests/authentication_pages_spec.rb:37:in `block (3 levels) in <
top (required)>'
3) Authentication with valid information
Failure/Error: it { should have_title(user.name) }
expected #has_title?("Michael Hartl") to return true, got false
# ./spec/requests/authentication_pages_spec.rb:36:in `block (3 levels) in <
top (required)>'
4) Authentication with valid information
Failure/Error: it { should_not have_link('Sign in', href: signin_path) }
expected #has_link?("Sign in", {:href=>"/signin"}) to return false, got t
rue
# ./spec/requests/authentication_pages_spec.rb:39:in `block (3 levels) in <
top (required)>'
它表示链接(“巧合地”嵌套在下拉菜单中的链接)没有返回 true。
rspec 规范中的路径与应用程序代码中使用的路径相同,您可以看到应用程序使用 rspec 中描述的相同路径/链接重定向您。
发生什么了?