使用Ruby on Rails教程问题进行集成测试

时间:2011-10-18 13:35:09

标签: ruby-on-rails ruby rspec integration-testing

我正在尝试按照ruby on rails教程进行整合测试。

我首先运行命令:bundle exec rspec spec/

它告诉我除了我的十六次测试之外的所有测试都通过了。以下是我认为问题所在的部分:

 require 'spec_helper'
 describe "LayoutLinks" do

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

结果我得到以下内容:

Failure/Error: response.should have_selector('title', :content => "Help")
   expected following output to contain a <title>Help</title> tag:
#home.html.erb page's source shown
<!DOCTYPE html>
   <html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
   <title>Ruby on Rails Tutorial Sample App | Home</title> 
   #The line above is why the test fails. 
   #It is loading home.html.erb instead of help.html.erb
   .
   .
   .
   # ./spec/requests/layout_links_spec.rb:34:in `block (2 levels) in <top (required)>'

我可以绕着测试的顺序移动,它始终是失败的顶级测试。这让我相信这里有一些问题而不是实际的rails代码。我也可以去演示网站,链接工作,他们转到正确的页面。我已经看过其他人对此的其他问题,我似乎找不到任何有同样问题的人。我怎么能解决这个问题?

更新:

noahc:sample_app noahc$ rake routes
users_new GET /users/new(.:format)     {:controller=>"users", :action=>"new"}
   signup     /signup(.:format)        {:controller=>"users", :action=>"new"}
  contact     /contact(.:format)       {:controller=>"pages", :action=>"contact"}
    about     /about(.:format)         {:controller=>"pages", :action=>"about"}
     help     /help(.:format)          {:controller=>"pages", :action=>"help"}
              /help(.:format)          {:controller=>"pages", :action=>"help"}
     root     /                        {:controller=>"pages", :action=>"home"}
pages_home GET /pages/home(.:format)    {:controller=>"pages", :action=>"home"}
pages_contact GET /pages/contact(.:format) {:controller=>"pages", :action=>"contact"}
pages_about GET /pages/about(.:format)   {:controller=>"pages", :action=>"about"}
pages_help GET /pages/help(.:format)    {:controller=>"pages", :action=>"help"}

2 个答案:

答案 0 :(得分:1)

诺亚,我建议将该测试分成多个测试,每个测试对应一个标题。这将使您更容易确切地指出错误的原因。像这样:

it "should have a link to 'Help'" do
  visit root_path
  response.should have_selector('a', :href => help_path, :content => "Help")
  click_link "Help"
  response.should have_selector('title', :content => 'Help')
end

it "should have a link to 'Contact'" do
  visit root_path
  response.should have_selector('a', :href => contact_path, :content => "Contact")
  click_link "Help"
  response.should have_selector('title', :content => 'Contact')
end

等......这将使您更容易准确查明正在发生的事情以及您遇到问题的位置。此外,它说的是在页面的来源中显示了什么?在解释了粘贴代码段中发生的事情之前,你切断了错误消息...查看实际返回的代码是什么,看看标题是什么以及它与rspec的期望有何不同之处。< / p>

答案 1 :(得分:1)

我不使用rspec,但可能因为“Home”!=“Ruby on Rails Tutorial Sample App | Home”

内容完全不匹配,因此测试失败。

另外,请检查您的来源中的帮助链接,以确保它实际上是您认为的位置...