2014-01-16 73 views
1

所以我一直試圖跟隨rails教程,但我一直堅持使標題略微動態。有一次,我去this part和運行Rails教程第3章Rspec測試失敗

$ bundle exec rspec spec/requests/static_pages_spec.rb 

我收到以下錯誤

Failures: 

    1) Static pages Help page should have the title 'Help' 
    Failure/Error: expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help") 
     expected #has_title?("Ruby on Rails Tutorial Sample App | Help") to return true, got false 
    # ./spec/requests/static_pages_spec.rb:27:in `block (3 levels) in <top (required)>' 

    2) Static pages Home page should have the title 'Home' 
    Failure/Error: expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home") 
     expected #has_title?("Ruby on Rails Tutorial Sample App | Home") to return true, got false 
    # ./spec/requests/static_pages_spec.rb:14:in `block (3 levels) in <top (required)>' 

    3) Static pages About page should have the title 'About Us' 
    Failure/Error: expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us") 
     expected #has_title?("Ruby on Rails Tutorial Sample App | About Us") to return true, got false 
    # ./spec/requests/static_pages_spec.rb:40:in `block (3 levels) in <top (required)>' 

Finished in 0.1826 seconds 
6 examples, 3 failures 

Failed examples: 

rspec ./spec/requests/static_pages_spec.rb:25 # Static pages Help page should have the title 'Help' 
rspec ./spec/requests/static_pages_spec.rb:12 # Static pages Home page should have the title 'Home' 
rspec ./spec/requests/static_pages_spec.rb:38 # Static pages About page should have the title 'About Us' 

這是我的家,幫助和關於意見等。

首頁

<% provide(:title, 'Home') %> 
<h1>Sample App</h1> 
<p> 
    This is the home page for the 
    <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> 
    sample application. 
</p> 

幫助

<% provide(:title, 'Help') %> 
<h1>Help</h1> 
<p> 
    Get help on the Ruby on Rails Tutorial at the 
    <a href="http://railstutorial.org/help">Rails Tutorial help page</a>. 
    To get help on this sample app, see the 
    <a href="http://railstutorial.org/book">Rails Tutorial book</a>. 
</p> 

關於

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

這是我的觀點應用

<!DOCTYPE html> 
<html> 
<head> 
    <title>Ruby on Rails Tutorial Sample App | <%= yield (:title) %></title> 
    <%= stylesheet_link_tag "application", media: "all", 
              "data-turbolinks-track" => true %> 
    <%= javascript_include_tag "application", "data-turbolinks-track" => true %> 
    <%= csrf_meta_tags %> 
</head> 
<body> 

<%= yield %> 

</body> 
</html> 

我的靜態頁面規格

require 'spec_helper' 

describe "Static pages" do 

    describe "Home page" do 

    it "should have the content 'Sample App'" do 
     visit '/static_pages/home' 
     expect(page).to have_content('Sample App') 
    end 

    it "should have the title 'Home'" do 
     visit '/static_pages/home' 
     expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home") 
    end 
    end 

    describe "Help page" do 

    it "should have the content 'Help'" do 
     visit '/static_pages/help' 
     expect(page).to have_content('Help') 
    end 

    it "should have the title 'Help'" do 
     visit '/static_pages/help' 
     expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help") 
    end 
    end 

    describe "About page" do 

    it "should have the content 'About Us'" do 
     visit '/static_pages/about' 
     expect(page).to have_content('About Us') 
    end 

    it "should have the title 'About Us'" do 
     visit '/static_pages/about' 
     expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us") 
    end 
    end 
end 

,最後我的Gemfile

source 'https://rubygems.org' 
ruby '2.1.0' 
#ruby-gemset=railstutorial_rails_4_0 

gem 'rails', '4.0.2' 

group :development, :test do 
    gem 'sqlite3', '1.3.8' 
    gem 'rspec-rails', '2.13.1' 
end 

group :test do 
    gem 'selenium-webdriver', '2.35.1' 
    gem 'capybara', '2.1.0' 
end 

gem 'sass-rails', '4.0.1' 
gem 'uglifier', '2.1.1' 
gem 'coffee-rails', '4.0.1' 
gem 'jquery-rails', '3.0.4' 
gem 'turbolinks', '1.1.1' 
gem 'jbuilder', '1.0.2' 

group :doc do 
    gem 'sdoc', '0.3.20', require: false 
end 

group :production do 
    gem 'pg', '0.15.1' 
    gem 'rails_12factor', '0.0.2' 
end 

回答

1

更改您的視圖像下面(通知=提供在第一行之前)。執行相同的所有三個視圖

首頁

<%= provide(:title, 'Home') %> 
<h1>Sample App</h1> 
<p> 
    This is the home page for the 
    <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> 
    sample application. 
</p> 
0

您是否嘗試過手動將每一頁,並檢查自己的稱號?

我粘貼了字面上你提供給項目的所有東西(我之前做過這個教程,所以我已經有一個和static_pages控制器一起設置)並且所有這些測試都通過了。我會說你可能混淆了你的視圖的文件名,或者路由文件中的名字,但是內容測試正在傳遞,所以不能這樣做。我想有可能你的一個文件被編輯器保存爲錯誤的編碼,或者其他的東西。在運行測試之前,請檢查是否已經保存了所有文件。

另外一個快速注意,=是不必要的<% provide ...所以你可能應該從你的看法中刪除它。

0

我想你可能已經忘記了在 '聯繫人' 頁面添加到您的routes.rb文件,像這樣:

SampleApp::Application.routes.draw do 
    get "static_pages/home" 
    get "static_pages/help" 
    get "static_pages/about" 
    get "static_pages/contact" 
0

試試這個:

static_pages_spec.rb

require 'spec_helper' 

describe "Static pages" do 

    let(:base_title) { "Ruby on Rails Tutorial Sample App" } 

    describe "Home page" do 

    it "should have the content 'Sample App'" do 
     visit '/static_pages/home' 
     expect(page).to have_content('Sample App') 
    end 

    it "should have the title 'Home'" do 
     visit '/static_pages/home' 
     #expect(page).to have_title("#{base_title} | Home") 
     page.should have_title("#{base_title} | Home") 
    end 
    end 

    describe "Help page" do 

    it "should have the content 'Help'" do 
     visit '/static_pages/help' 
     expect(page).to have_content('Help') 
    end 

    it "should have the title 'Help'" do 
     visit '/static_pages/help' 
     #expect(page).to have_title("#{base_title} | Help") 
     page.should have_title("#{base_title} | Help") 
    end 
    end 

    describe "About page" do 

    it "should have the content 'About Us'" do 
     visit '/static_pages/about' 
     expect(page).to have_content('About Us') 
    end 

    it "should have the title 'About Us'" do 
     visit '/static_pages/about' 
     page.should have_title("#{base_title} | About Us") 
    end 
    end 

    describe "Contact page" do 

    it "should have the content 'Contact'" do 
     visit '/static_pages/contact' 
     expect(page).to have_content('Contact') 
    end 

    it "should have the title 'Contact'" do 
     visit '/static_pages/contact' 
    # expect(page).to have_title("#{base_title} | Contact") 
     page.should have_title("#{base_title} | Contact") 
    end 
    end 
end 

應用.html.erb

<!DOCTYPE html> 
    <html> 
    <head> 
    <title>Ruby on Rails Tutorial Sample App | <%= yield(:title)%></title> 
    <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> 
    <%= javascript_include_tag "application", "data-turbolinks-track" => true %> 
    <%= csrf_meta_tags %> 
    </head> 
    <body> 
<%= yield %> 

</body> 
</html> 

寶石'水豚','2.1。0'

暱稱中的空格

相關問題