rails教程期望css标题与文本返回一些东西

我正在研究ruby.railstutorial.org上的RoR教程,目前正在第4章。测试没有validation,我不明白为什么。 我的spec文件的顶部部分看起来像这样。 其他方法看起来与帮助相同:

describe "StaticPages" do let(:page_title) {"Ruby on Rails Tutorial Sample App"} let(:h1_test) {"should have the h1"} let(:title_test) {"should have the title"} describe "Home page" do |title_test, h1_test| it "#{h1_test} 'Sample App'" do visit '/static_pages/home' page.should have_selector('h1',:text=>'Sample App') end it "#{title_test} 'Home'" do visit '/static_pages/home' page.should have_selector('title', :text=> '#{page_title}') end it "should not have a custom page title" do visit '/static_pages/home' page.should_not have_selector('title', :text=> '| Home') end end describe "Help page" do |title_test, h1_test| it "#{h1_test} 'Help'" do visit '/static_pages/help' page.should have_selector('h1', :text =>'Help') end it "#{title_test} 'Help'" do visit '/static_pages/help' page.should have_selector('title', :text=>'#{page_title} | Help') end end 

我的application_helper.rb:

 module ApplicationHelper def full_title(page_title) base_title = "Ruby on Rails Tutorial Sample App" if page_title.empty? base_title else "#{base_title} | #{page_title}" end end end 

我的application.html.haml文件:

 !!! %html %head %title= full_title(yield(:title)) = stylesheet_link_tag "application", :media => "all" = javascript_include_tag "application" = csrf_meta_tags %body= yield 

现在4个错误是相同的,减去一个单词的变化,所以我只发一个:

  1) StaticPages Contact# 'Contact' Failure/Error: page.should have_selector('title', :text=>'#{page_title} | Contact') expected css "title" with text "\#{page_title} | Contact" to return something # ./spec/requests/static_pages_spec.rb:65:in `block (3 levels) in ' 

我很神秘,因为我只是直接从指南中复制它,而不是使用haml并在我的spec文件中添加前几行以最小化重复文本。 我没有包含页面html文件,因为我看不出它们是怎么回事。 特别是因为home.html.haml没有像其他人一样- provide(:title, 'Home')但仍然抛出与上面相同的错误。 任何帮助将不胜感激!

编辑:这是我的Gemfile:

  gem 'rails', '3.2.12' gem 'haml', '4.0.0' group :development, :test do gem 'sqlite3', '1.3.5' gem 'rspec-rails', '2.11.0' gem 'haml-rails', '0.4' gem 'guard-rspec', '1.2.1' gem 'guard-spork', '1.4.2' gem 'spork', '0.9.2' end group :assets do gem 'sass-rails', '3.2.5' gem 'coffee-rails','3.2.2' gem 'uglifier', '1.2.3' end gem 'jquery-rails', '2.0.2' group :test do gem 'capybara', '1.1.2' gem 'rb-inotify', '0.8.8' gem 'libnotify', '0.5.9' end group :production do gem 'pg', '0.12.2' end 

确保您使用的是Capybara版本1.* (在教程中使用)。 从2.0开始的新版Capybara改变了它检查页面内容的方式。 现在建设

 page.should have_selector(selector, :text=> text) 

仅适用于可见DOM元素( h1pspan等),但不适用于titlescript等不可见元素。

您可以在Gemfile设置以前的Gemfile版本

 group :test do gem 'capybara', '1.1.2' end 

或修改您的测试:

 page.source.should have_selector(selector, :text=> text) 

我解决了这个问题。 我说'#{page_title}'地方应该是"#{page_title}" 。 双引号而不是单引号-_-。 有人能告诉我为什么这样有效吗? 特别是因为我在相同的have_selector检查中有类似'Sample App'东西,并且不会抛出任何类型的错误。 希望这至少可以帮助将来的某个人。

Interesting Posts