2

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

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

1 回答 1

0

我想问题出在这里:

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

这只是我的一个观点,我不擅长测试。

于 2013-07-16T16:00:50.773 回答