2

我正在学习 Michael Hartl 的 Ruby on Rails 教程,并且正在做第 3 章的练习。有人可以解释为什么这个测试失败了吗?

我遇到了失败

rspec ./spec/requests/static_pages_spec.rb:39 # 
Static pages About page should have the title 'About Us'

控制器

  class StaticPagesController < ApplicationController
  def home
  end

  def help
  end

  def about
  end

  def Contact
  end
end

关于.html.erb

<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | About Us</title>
</head>
<body>
<h1>About Us</h1>

Spec.rb

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 Us'" do
  visit '/static_pages/about'
  page.should have_selector('title',
                :text => "Ruby on Rails Tutorial Sample App | About Us")
  end
end 

路由.rb

SampleApp::Application.routes.draw do
  get "static_pages/home"

  get "static_pages/help"

  get "static_pages/about"

  get "static_pages/Contact"
end
4

2 回答 2

0

尝试

page.should have_xpath("//title", :text => "About Us")
于 2012-09-30T04:24:38.890 回答
0

如果您正在关注 MHartl 的教程并且您正在使用 gem 'Capybara',我可以确认将您的 ':text => ...' 更改为 ':content => ...' 将使您的测试通过。感谢@郭吉米

于 2013-04-09T18:53:31.630 回答