我有一个 rspec 测试失败。
1) User pages edit with valid information should have link "Sign out", {:href=>"/signout"}
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/user_pages_spec.rb:80:in `block (4 levels) in <top (required)>'
测试查看登录后标题中是否存在“退出”链接。代码在浏览器中有效,但在 rspec 中无效。有问题的规范与 Hartl 教程一致:
describe "User pages" do
subject { page }
...
describe "edit" do
let(:user) { FactoryGirl.create(:user) }
before { visit edit_user_path(user) }
...
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_title(new_name) }
it { should have_selector('div.alert.alert-success') }
it { should have_link('Sign out', href: signout_path) }
specify { expect(user.reload.name).to eq new_name }
specify { expect(user.reload.email).to eq new_email }
end
end
end
以下代码是正在测试的标头模板(也遵循 Hartl Rails 教程)。我已经运行了一些测试(使用puts
),发现登录后,该signed_in?
函数向 rspec 返回 false(在浏览器中测试时返回 true)。
<% 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", edit_user_path(current_user) %></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 %>
看来这是 rspec 的一个问题,我无法弄清楚如何补救。
在此先感谢您的帮助。