身份验证页面规范有效信息测试失败

时间:2013-10-20 02:52:15

标签: ruby-on-rails ruby tdd capybara

我正在关注Michael Hart'l RoR教程,但似乎在使用有效信息进行authentication_pages测试时遇到问题要通过第8章。

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

测试如下:失败者是“有效信息” - 除了它之外的一切{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"}

这是登录后页面的屏幕截图 enter image description here

其中一个问题是我现在无法退出!! (我还没有完成实际启用注销的教程中的内容。)

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

1 个答案:

答案 0 :(得分:1)

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

做以下事情

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

OR

表示'form_for'案例

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

3)检查按钮上的标题是否符合规范