我正在研究 ruby.railstutorial.org 上的 RoR 教程,目前正在阅读第 4 章。测试没有验证,我不知道为什么。我的规范文件的顶部看起来像这样。其他方法看起来都与 Help 相同:
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#<Class:0x0000000446e818> '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 <top (required)>'
我很困惑,因为我只是直接从指南中复制它,而不是使用 haml 并在我的规范文件中添加前几行以最小化重复文本。我没有包含页面 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