0

我正在关注 Michael Hart'l RoR 教程,但似乎无法让 authentication_pages 测试“使用有效信息”在第 8 章中通过。

具体来说,测试抱怨页面中缺少相关链接,即使当我实际访问该页面时,它们也很好。事实上,即使我将这些链接硬插入每个页面,测试也会抱怨相关链接不存在。

测试如下:失败的是“具有有效信息”的测试 - 除了 it { should have_title(user.name) }

    require 'spec_helper'

describe "AuthenticationPages" do

    subject {page}

    describe "signin page" do
        before {visit signin_path}
        it {should have_content("Sign in")}
        it {should have_title("Sign in")}

        describe "with invalid information" do
            before {click_button('Sign in')}
            it {should have_title('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", text: "Invalid")}
            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_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) }
      it { should have_content('This is the user page')}**
    end
  end
end

这是错误消息:

......................................FFFF

Failures:

  1) AuthenticationPages signin page 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/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'

  2) AuthenticationPages signin page with valid information should have content "This is the user page"
     Failure/Error: it { should have_content('This is the user page')}
       expected #has_content?("This is the user page") to return true, got false
     # ./spec/requests/authentication_pages_spec.rb:36:in `block (4 levels) in <top (required)>'

  3) AuthenticationPages signin page with valid information should not have link "Sign in", {:href=>"/signin"}
     Failure/Error: it { should_not have_link('Sign in', href: signin_path) }
       expected #has_link?("Sign in", {:href=>"/signin"}) to return false, got true
     # ./spec/requests/authentication_pages_spec.rb:35:in `block (4 levels) in <top (required)>'

  4) AuthenticationPages signin page with valid information should have link "Profile", {:href=>"/users/1"}
     Failure/Error: it { should have_link('Profile',     href: user_path(user)) }
       expected #has_link?("Profile", {:href=>"/users/1"}) to return true, got false
     # ./spec/requests/authentication_pages_spec.rb:33:in `block (4 levels) in <top (required)>'

Finished in 0.74198 seconds
42 examples, 4 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:34 # AuthenticationPages signin page with valid information should have link "Sign out", {:href=>"/signout"}
rspec ./spec/requests/authentication_pages_spec.rb:36 # AuthenticationPages signin page with valid information should have content "This is the user page"
rspec ./spec/requests/authentication_pages_spec.rb:35 # AuthenticationPages signin page with valid information should not have link "Sign in", {:href=>"/signin"}
rspec ./spec/requests/authentication_pages_spec.rb:33 # AuthenticationPages signin page with valid information should have link "Profile", {:href=>"/users/1"}

这是登录后的页面截图 在此处输入图像描述

问题之一是我现在无法退出!!(我还没有了解您实际启用注销的教程)。

这是 _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

1

您的规范和 _header 看起来不错。虽然您可以手动登录,但问题在于如何将数据传递给 Capybara,因为测试结果表明登录不成功。

做以下事情

0) 重新启动测试数据库

$ bundle exec rake test:prepare

1) 在 factory.rb 中检查工厂

FactoryGirl.define do
  factory :user do
    name     "John Smith"
    email    "john@example.com"
    password "foobar"
    password_confirmation "foobar"
  end
end

2) 检查 '/app/views/sessions/new.html.erb 中的字段名称。他们应该看起来像

对于“form_tag”案例

<%= f.label :email %>

或者

对于“form_for”案例

<%= label_tag :email, "Email" %>

3) 检查按钮上的标题是否与规范中的标题匹配

于 2013-10-20T16:49:16.360 回答