【问题标题】:Ruby Rails Hartl Tutorial Ch. 8.2.6 Test FailuresRuby Rails Hartl 教程 Ch. 8.2.6 测试失败
【发布时间】:2013-07-17 04:53:46
【问题描述】:

我是 Rails 的新手,也是 Michael Hartl 教程的一部分,所有测试都应该通过;就在 8.30 之前,我遇到了这些故障。我添加了身份验证规范和会话助手;非常感谢任何帮助。

1) Authentication signin with valid information 
 Failure/Error: it { should have_link('Settings', href: edit_user_path(user)) }
   expected link "Settings" to return something
 # ./spec/requests/authentication_pages_spec.rb:41:in `block (4 levels) in <top (required)>'

2) Authentication signin with valid information 
 Failure/Error: it { should have_link('Users', href: users_path) }
   expected link "Users" to return something
 # ./spec/requests/authentication_pages_spec.rb:39:in `block (4 levels) in <top (required)>'

Finished in 2.23 seconds
56 examples, 2 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:41 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:39 # Authentication signin with valid information

Authentication_pages_spec

require 'spec_helper'

describe "Authentication" do

  subject { page }

  describe "signin page" do
    before { visit signin_path }

    it { should have_selector('h1',    text: 'Sign in') }
    it { should have_selector('title', text: 'Sign in') }
  end

  describe "signin" do
    before { visit signin_path }

    describe "with invalid information" do
      before { click_button "Sign in" }

      it { should have_selector('title', text: 'Sign in') }
      it { should have_selector('div.alert.alert-error', text: 'Invalid') }

      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_selector('title', text: user.name) }

      it { should have_link('Users',    href: users_path) }
      it { should have_link('Profile',  href: user_path(user)) }
      it { should have_link('Settings', href: edit_user_path(user)) }
      it { should have_link('Sign out', href: signout_path) }
      it { should_not have_link('Sign in', href: signin_path) }
      end
    end
  end

sessions_helper.rb

    module SessionsHelper

  def sign_in(user)
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user
  end

  def signed_in?
    !current_user.nil?
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
  end
end

_header.html.erb

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

【问题讨论】:

  • 您还应该粘贴您的视图代码。
  • @MarekLipka 不太确定我应该发布哪一个,但我更新了我的问题

标签: ruby-on-rails ruby railstutorial.org


【解决方案1】:

我猜问题出在这里:

<li><%= link_to "Users", '#' %></li>
<li><%= link_to "Settings", '#' %></li>

测试清楚地说明了

expected link "Settings" to return something
expected link "Users" to return something

但是你的两个链接路径都是nil#,你应该设置链接的路径:

<%= link_to "Settings", edit_user_path(current_user) %>
<%= link_to "Users", users_path %>

这只是我的一个看法,我不擅长测试。

【讨论】:

  • 我这样做了,但得到的错误比我开始时更多。而且本教程没有告诉我添加这些路径。如果我添加更多代码,它可能是我没有粘贴但我只是不知道要发布哪些视图或文件的文件
  • 很高兴看到您遇到的那些错误。我也犯了一个错误,应该是 current_user 而不是 user。
  • 对于其他设置了路径的链接,您的测试正在通过并且仅对于具有 # 作为路径(nil 路径)但在测试中需要 @ 987654326@ 和 it { should have_link('Users', href: users_path) } 查看我编辑的链接,然后重试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多