Rail3教程测试(第5章):click_link错误

时间:2011-07-14 23:40:15

标签: ruby-on-rails

交叉发布到:http://www.ruby-forum.com/topic/2140862#new

以下是我遇到的错误:

.....F

Failures:
  1) LayoutLinks should have the right links on the layout
     Failure/Error: click_link "Sign up"
     Could not find link with text or title or id "Sign up"
     # ./spec/requests/layout_links_spec.rb:37:in `block (2 levels) in
     <top (required)>'

Finished in 0.28358 seconds
6 examples, 1 failure

以下是LayoutLinks的测试文件。最后一个'阻止'有了 click_link给我的问题:

#规格/请求/ layout_links_spec.rb:

require 'spec_helper'

describe "LayoutLinks" do
  it "should have a Home page at: /" do
    get '/'
    response.should have_selector('title', :content => "Home")
  end

  it "should have an About page at: /about" do
    get '/about'
    response.should have_selector('title', :content => "About")
  end

  it "should have a Contact page at: /contact" do
    get '/contact'
    response.should have_selector('title', :content => "Contact")
  end

  it "should have a Help page at: /help" do
    get '/help'
    response.should have_selector('title', :content => "Help")
  end

  it "should have a Sign up page at: /signup" do
    get "/signup"
    response.should have_selector('title', :content => "Sign up")
  end

  it "should have the right links on the layout" do
    visit root_path
    click_link "About"
    response.should have_selector('title', :content => "About")
    click_link "Contact"
    response.should have_selector('title', :content => "Contact")
    click_link "Help"
    response.should have_selector('title', :content => "Help")
    click_link "Sign up"
    response.should have_selector('title', :content => "Sign up")
    click_link "Home"
    response.should have_selector('title', :content => "Home")
  end

其他click_link测试通过。这只是“注册”链接 这给了我这个问题。我能辨别的唯一区别是 具有“注册”链接的页面由不同的页面处理 控制器:用户#new v。页面(控制器在下面)。

以下是我的路线:

配置/ routes.rb中:

SampleApp::Application.routes.draw do
  get "users/new"

  match '/signup', :to => 'users#new'

  match '/about', :to => 'pages#about'
  match '/contact', :to => 'pages#contact'
  match '/help', :to => 'pages#help'

  root :to => 'pages#home'
end

以下是应用程序布局:

应用程序/视图/布局/ application.html.erb:

<!DOCTYPE html>
<html>
  <head>
    <title><%= title() %></title>
    <%= csrf_meta_tag %>
    <%= render 'layouts/stylesheets' %>
  </head>

  <body>

    <div class="container">

      <%= render 'layouts/header' %>

      <section class="round">
        <%= yield %>
      </section>

      <%= render 'layouts/footer' %>

    </div>

  </body>
</html>

以下是包含“注册”链接的主页,该链接在上面的应用程序布局中的“yield”处插入:

应用程序/视图/页/ home.html.erb:

<h1>Me</h1>
<p>Hi, this is my website.</p>

<%= link_to "Sign up", signup_path, :class => "signup_button round" %>

这是我的控制器:

应用程序/控制器/ users_controller.rb:

class UsersController < ApplicationController
  def new
    @title = "Sign up"
  end

end

应用程序/控制器/ pages_controller.rb:

class PagesController < ApplicationController
  def home
    @title = "Home"
  end

  def contact
    @title = "Contact"
  end

  def about
    @title = "About"
  end

  def help
    @title = "Help"
  end


end

当我手动点击主页上的“注册”链接时,它会成功显示页面:

应用程序/视图/用户/ new.html.erb

<h1>Users#new</h1>
<p>Find me in app/views/users/new.html.erb</p>

如果我点击主页的查看源,则会出现“注册”链接(不要与“登录”链接混淆):

<!DOCTYPE html>
<html>
  <head>
    <title>My website | Home</title>
    <meta name="csrf-param" content="authenticity_token"/>
<meta name="csrf-token" content="i/PD5uKjp87ArBPcr51a82KeW/5peacAJ6M908f3sP8="/>
    <!--[if lt IE 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

<link href="/stylesheets/blueprint/screen.css?1310454155" media="screen" rel="stylesheet" type="text/css" />
<link href="/stylesheets/blueprint/print.css?1310454155" media="print" rel="stylesheet" type="text/css" />

<!--[if lt IE 8]>
  <link href="/stylesheets/blueprint/ie.css?1310454155" media="screen" rel="stylesheet" type="text/css" />
<![endif]-->

<link href="/stylesheets/custom.css?1310632912" media="screen" rel="stylesheet" type="text/css" />

  </head>

  <body>

    <div class="container">

      <header>
  <a href="/"><img alt="Autumn" class="round" height="140" src="/images/autumn.jpg?1310626333" width="300" /></a>

  <nav class="round">
    <ul>
      <li><a href="/">Home</a></li>
      <li><a href="/help">Help</a></li>
      <li><a href="#">Sign in</a></li>
    </ul>
  </nav>

</header>


      <section class="round">
        <h1>Me</h1>
<p>Hi, this is my website.</p>

<a href="/signup" class="signup_button round">Sign up</a>



      </section>

      <footer>
  <nav class="round">
    <ul>
      <li><a href="/about">About</a></li>
      <li><a href="/contact">Contact</a></li>
      <li><a href="http://news.railstutorial.org/">News</a></li>
    </ul>
  </nav>
</footer>



    </div>

  </body>
</html>

2 个答案:

答案 0 :(得分:2)

click_link实际上会更改测试正在检查的页面,即在您完成click_link后,“visit root_path”不再是当前页面。我的“注册”链接仅显示在主页上,而其他链接显示在每个页面上。因此,导航离开主页,然后要求测试点击“注册”链接不起作用。

解决方案就是把这个:

click_link "Sign up"
response.should have_selector('title', :content => "Sign up")

之后:

visit root_path

或之后:

click_link "Home"
response.should have_selector('title', :content => "Home")

答案 1 :(得分:0)

使用大量click_link测试时测试的顺序非常重要。正如7stud指出的那样,click_link模拟实际点击链接并转到链接指向的任何地方。

确保您正在测试的链接存在于您正在进行的页面上,否则您将收到错误。

您可以执行其他答案建议的内容,或

visit root_path

然后

click_link "Sign up"