简体   繁体   English

Michael Hartl的Ruby on Rails教程。 第9.1章中的失败测试

[英]Ruby on Rails Tutorial by Michael Hartl. Failing Test in Chapter 9.1

Hi i'm working through the Hartl's Ruby on Rails Tutorial and i'm stuck with one failing test in Chapter 9.1 of. 嗨,我正在完成Hartl的Ruby on Rails教程,我在第9.1章中遇到了一个失败的测试。 When I run spec in terminal it returns: 当我在终端中运行spec时,它返回:

sis-macbook-pro:sample_app Lagaspi$ bundle exec rspec spec/
...............................F................................

Failures:

1) AuthenticationPages 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:37:in `block (4 levels) in <top (required)>'

Finished in 1.42 seconds
64 examples, 1 failure

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:37 # AuthenticationPages signin with valid information

Here's my code - authentication_pages_spec.rb 这是我的代码 - authentication_pages_spec.rb

require 'spec_helper'

describe "AuthenticationPages" 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 { sign_in user }

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

  describe "followed by signout" do
    before { click_link "Sign out" }
    it { should have_link('Sign in') }
  end
end
end
end

And I think this is the problem line (37): 我认为这是问题线(37):

it { should have_link('Users',       href: users_path) }

But what should I do? 但是我该怎么办? I'm a newbie and can't figure it out. 我是新手,无法弄清楚。 Thanks Si. 谢谢你。

If you're following the tutorial, the link should only be visible if a user is logged in. You haven't eluded to any of the other tests failing when the user is logged in, ie 如果您正在学习本教程,则只有在用户登录时才能看到该链接。当用户登录时,您没有想到任何其他测试失败,即

it { should have_link('Profile',     href: user_path(user)) }
it { should have_link('Sign out',    href: signout_path) }
it { should have_link('Settings',    href: edit_user_path(user)) }

And so with that in mind I'm inclined to think that the issue is in the view, not the spec. 因此,考虑到这一点,我倾向于认为问题在于视图,而不是规范。 At a glance, the spec looks fine. 一目了然,规格看起来很好。

Does your _header.html.erb include this 你的_header.html.erb是否包括这个

...
<% if signed_in? %>
  <li><%= link_to "Users", users_path %></li>
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM