3

我在使用 rspec 时遇到了这个错误。但是,代码应该可以通过。Rspec 抱怨的所有选择器都存在。

FF.FFFFFF

Failures:

  1) StaticPages Home page should have the h1 'Sample App'
     Failure/Error: page.should have_selector('h1', text:  'Sample App')
       expected css "h1" with text "Sample App" to return something
     # ./spec/requests/static_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

  2) StaticPages Home page should have the base title
     Failure/Error: page.should have_selector('title', text:  "Ruby on Rails Tutorial Sample App")
       expected css "title" with text "Ruby on Rails Tutorial Sample App" to return something
     # ./spec/requests/static_pages_spec.rb:14:in `block (3 levels) in <top (required)>'

  3) StaticPages Help page should have the h1  'Help'
     Failure/Error: page.should have_selector('h1',text:  "Help")
       expected css "h1" with text "Help" to return something
     # ./spec/requests/static_pages_spec.rb:28:in `block (3 levels) in <top (required)>'

  4) StaticPages Help page should have the title 'Help'
     Failure/Error: page.should have_selector('title', text:  "Ruby on Rails Tutorial Sample App | Help")
       expected css "title" with text "Ruby on Rails Tutorial Sample App | Help" to return something
     # ./spec/requests/static_pages_spec.rb:33:in `block (3 levels) in <top (required)>'

  5) StaticPages About page should have the h1 'About us' 
     Failure/Error: page.should have_selector('h1', text:  'About Us')
       expected css "h1" with text "About Us" to return something
     # ./spec/requests/static_pages_spec.rb:41:in `block (3 levels) in <top (required)>'

  6) StaticPages About page should have the title 'About Us'
     Failure/Error: page.should have_selector('title', text:  "Ruby on Rails Tutorial Sample App | About Us")
       expected css "title" with text "Ruby on Rails Tutorial Sample App | About Us" to return something
     # ./spec/requests/static_pages_spec.rb:46:in `block (3 levels) in <top (required)>'

  7) StaticPages Contact pages should have the h1 'Contact'
     Failure/Error: page.should have_selector('h1', text:  'Contact')
       expected css "h1" with text "Contact" to return something
     # ./spec/requests/static_pages_spec.rb:53:in `block (3 levels) in <top (required)>'

  8) StaticPages Contact pages Should have the title page 'Contact'
     Failure/Error: page.should have_selector('title',
       expected css "title" with text "Ruby on Rails Tutorial Sample App | Contact" to return something
     # ./spec/requests/static_pages_spec.rb:59:in `block (3 levels) in <top (required)>'

Finished in 0.31051 seconds
9 examples, 8 failures

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:7 # StaticPages Home page should have the h1 'Sample App'
rspec ./spec/requests/static_pages_spec.rb:12 # StaticPages Home page should have the base title
rspec ./spec/requests/static_pages_spec.rb:26 # StaticPages Help page should have the h1  'Help'
rspec ./spec/requests/static_pages_spec.rb:31 # StaticPages Help page should have the title 'Help'
rspec ./spec/requests/static_pages_spec.rb:39 # StaticPages About page should have the h1 'About us' 
rspec ./spec/requests/static_pages_spec.rb:44 # StaticPages About page should have the title 'About Us'
rspec ./spec/requests/static_pages_spec.rb:51 # StaticPages Contact pages should have the h1 'Contact'
rspec ./spec/requests/static_pages_spec.rb:56 # StaticPages Contact pages Should have the title page 'Contact'

这是规格

require 'spec_helper'

describe "StaticPages" do

  describe "Home page" do

    it "should have the h1 'Sample App'" do
     get '/static_pages/home'
      page.should have_selector('h1', text:  'Sample App')
    end

    it "should have the base title" do
      get '/static_pages/home'
      page.should have_selector('title', text:  "Ruby on Rails Tutorial Sample App")
    end

    it "should not havea custom page title " do
      get '/static_pages/home'
      page.should_not have_selector('title', text:  '| Home')
    end
  end
  #end home page spec

  describe "Help page" do

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

    it "should have the title 'Help'" do
      get '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
      get '/static_pages/about'
      page.should have_selector('h1', text:  'About Us')
    end

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

  describe "Contact pages" do
    it "should have the h1 'Contact'" do
      get '/static_pages/contact'
      page.should have_selector('h1', text:  'Contact')
    end

    it "Should have the title page 'Contact'" do
      get '/static_pages/contact'
      #this test is passing but the code in the book is not the same as what is below
      page.should have_selector('title',
                                    text:  "Ruby on Rails Tutorial Sample App | Contact")
    end
  end
end

home.html.erb 视图

<div> 
  <h1>Welcome to the Sample App</h1>
    <h2>This is the home page for the
      <a href="http://railstutorial.org">Ruby on Rails Tutorial</a>
      sample application
    <h2>
      <%= link_to "Sign up now!", '#', class: "btn btn-large btn-primary"%>
 </div> 

 <%= link_to image_tag("rails.png", alt:  "Rails"), 'http://rubyonrails.org' %>

contact.html.erb

<% provide(:title, 'Contact' ) %>
<h1>Contact</h1>
<p>
  Contact Ruby on rails tutorial about he sample app at the 
  <a href="http://railstutorial.org/contact">contact page</a>
</p>

about.html.erb

<% provide(:title,'About Us') %>
  <h1>About Us</h1>
  <p>
    <a href="http://railstutorial.orb">Ruby on Rails Tutorial</a>
    is a project to make a book and screencasts to teach web development 
    <a href="http://rubyonrails.org">Ruby on Rails</a> This
    is the sampel app for the tutorial
  </p>
4

1 回答 1

0

而是get在测试中尝试使用visit方法:http ://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session#visit-instance_method

于 2012-05-27T21:41:37.617 回答