rails tutorial expected css title with text to return something

好久不见. 提交于 2020-01-24 01:24:09

问题


I am working through the RoR tutorial at ruby.railstutorial.org and am currently on chapter 4. The tests aren't validating and I have no idea why. Top part of my spec file looks like this. The other methods all look identical to 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

My 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

My application.html.haml file:

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

Now the 4 errors are identical minus one word change, so I'll just post one:

  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)>'

I am mystified because I am just copying it straight from the guide other than using haml and adding in those top few lines in my spec file to minimize repeat text. I didn't include the page html files because I can't see how they could be the problem. Especially since the home.html.haml doesn't have a - provide(:title, 'Home') like the others but is still throwing the same error as above. Any help would be greatly appreciated!

Edit: Here is my 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

回答1:


Make sure you are using Capybara version 1.* (as used in the tutorial). New versions of Capybara, starting from 2.0, change the way it checks page content. Now construction

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

works only for visible DOM elements (h1, p, span, etc.), but does not work for non-visible elements like title or script.

You can set previous Capybara version in the Gemfile

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

Or modify your tests:

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



回答2:


I figured out the problem. Where I say '#{page_title}' it should be "#{page_title}". Double quotes instead of single quotes -_-. Can someone tell me why that works? Especially because I have stuff like 'Sample App' in the same have_selector checks and that doesn't throw any kind of error. Hope this helps someone in the future at least.



来源:https://stackoverflow.com/questions/14921541/rails-tutorial-expected-css-title-with-text-to-return-something

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!