登录?函数在 Rails 教程 (Hartl) 的第 8-9 章中不起作用 [英] signed_in? function not working in Chapter 8-9 of Rails Tutorial (Hartl)

查看:49
本文介绍了登录?函数在 Rails 教程 (Hartl) 的第 8-9 章中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rspec 测试失败.

I have one rspec test failing.

  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 教程一致:

The test looks to see if a 'Sign out' link is present in the header after signing in. The code works in a browser but not in rspec. The spec in question is aligned with the Hartl tutorial:

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).

The following code is the header template being tested (also follows the Hartl Rails tutorial). I've run some tests (using puts) and found out that after logging in, the signed_in? function is returning false to rspec (while returning true when tested in the browser).

        <% 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 的问题,我一直无法弄清楚如何补救.

It appears this is a problem with rspec and I haven't been able to figure out what to do to remedy.

预先感谢您的帮助.

推荐答案

更新:这个答案的原始版本侧重于清单 9.1,结果证明它是正确的,并且从那以后一直是正确的由于 删除导致的其他问题.但是,OP 的摘要评论中指出的清单 9.9 的问题是一个真正的问题,将 sign_in user 添加到该清单的修复程序仍然存在.

Update: The original version of this answer focused on Listing 9.1, which turned out to have been correct as is and has since been reverted to its current state with no sign_in user due to other problems the removal caused. However, the issue with Listing 9.9 noted in the OP's summary comment was a bona fide problem and the fix to add sign_in user to that listing remains.

这似乎是 Rails 中的一个错误4 教程,从之前版本中存在的清单 9.9 中的 before 块中删除了 sign_in(user) 行.此后已更正.

This does appear to have been a bug in the Rails 4 tutorial, which dropped the sign_in(user) line from the before block in listing 9.9 that was present in the previous version. It has since been corrected.

这篇关于登录?函数在 Rails 教程 (Hartl) 的第 8-9 章中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆