2012-11-20 32 views
2

Im遵循Michael Hartl的Ruby On Rails 3教程和使用Capybara的集成规范。整合规范至今如下标题测试失败的请求规格在迈克尔哈特斯教程Rails 3

require 'spec_helper' 

describe "StaticPages" do 
    describe "Home page" do 
    it "should have the h1 'Sample App'" do 
     visit '/static_pages/home' 
     page.should have_selector('h1',:text => 'Sample App') 
    end 

    it "should have the title 'Home'" do 
     visit '/static_pages/home' 
     page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | Home") 
    end 
    end 

    describe "Help page" do 
    it "should have the h1 'Help'" do 
     visit '/static_pages/help' 
     page.should have_selector('h1',:text => 'Help') 
    end 

    it "should have the title 'Help'" do 
     visit '/static_pages/help' 
     page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | Help") 
    end 
    end 

    describe "About page" do 
    it "should have the h1 'About Us'" do 
     visit '/static_pages/about' 
     page.should have_selector('h1',:text => 'About Us') 
    end 

    it "should have the title 'About'" do 
     visit '/static_pages/about' 
     page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | About Us") 
    end 
    end 
end 

当我运行这些测试,我得到:

1) StaticPages Home page should have the title 'Home' 
    Failure/Error: page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | Home") 
     expected #has_selector?("title") to return true, got false 
    # ./spec/requests/static_pages_spec.rb:12:in `block (3 levels) in <top (required)>' 

    2) StaticPages Help page should have the title 'Help' 
    Failure/Error: page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | Help") 
     expected #has_selector?("title") to return true, got false 
    # ./spec/requests/static_pages_spec.rb:24:in `block (3 levels) in <top (required)>' 

    3) StaticPages About page should have the title 'About' 
    Failure/Error: page.should have_selector('title',:text => "Ruby on Rails Tutorial Sample App | About Us") 
     expected #has_selector?("title") to return true, got false 
    # ./spec/requests/static_pages_spec.rb:36:in `block (3 levels) in <top (required)>' 

我预计职称考试的帮助和即将失败的页面,但我home.html.erb如下所示

<html> 
<head> 
    <title>Ruby on Rails Tutorial Sample App | Home</title> 
</head> 
<body> 
<h1>Sample App</h1> 
<p> 
This is the homepage for the sample app 
</p> 
</body> 
</html> 

另外,我看到标题'Ruby on Rails Tutorial Sample App |首页'on'/ static_pages/home'。什么导致家庭冠军测试失败?

+0

请在下面标记Torstein的回答作为接受的答案。 – petsagouris

回答

4

在你的Gemfile,改变

gem 'capybara' 

gem 'capybara', '1.1.2' 

并运行 '捆绑更新'。

+0

非常感谢。某人如何用水豚2实现同样的目标? – petsagouris

+0

尝试'page.should have_selector('title',:content =>'Sample App')'。 (:文字已被替换为:内容。)这也可能解决原始问题。 – Torstein

+1

感谢您花时间回复评论。不幸的是,与水豚2.0.2我得到的ArgumentError:无效的键:内容,应该是:文本,:可见,:之间,:计数,:最大值,:最小 – petsagouris

6

水豚2.1改变了对查询标题元素的支持。因此,使用选择器以这种方式查询html文档头部的标题元素将失败“page.should have_selector('title',:text =>'Some text')

使用“page.should have_title(‘一些文本’)”如果使用水豚2X其建议查询标题元素应该工作,这是2.1 API实现查询标题元素新方法。

而且将位于'spec'文件夹(spec /文件夹)中名为'requests'的子文件夹中的文件移动到名为'features'(spec/features)的新文件夹中。

希望这项工作。

快乐编码!

+0

这应该是接受的答案 – tim

相关问题