2013-08-01 156 views
1

我是一個總新手,我正在學習邁克爾哈特爾的教程Ruby on Rails。我在第3章,關於頁面的Rspec測試失敗(雖然完全相同的測試對於主頁和幫助頁面並不失敗)。簡單的Rspec測試失敗邁克爾哈特爾RoR Tuotiral

運行$ bundle exec rspec spec/requests/static_pages_spec.rb當我得到的錯誤是:

Failures: 

1) Static pages About page should have the content 'About Us'←[31mFailure/Error:←[0m ←[31mvisit 'static_pages/about'←[0m←[31mURI::InvalidURIError←[0m:←[31mthe scheme http does not accept registry part: www.example.com:80static_pages (or bad hostname?)←[0m←[36m  # ./spec/requests/static_pages_spec.rb:24:in `block (3 levels) in <top (required)>'←[0m Finished in 0.0776 seconds←[31m3 examples, 1 failure←[0m 

Failed examples: 

←[31mrspec ./spec/requests/static_pages_spec.rb:23←[0m ←[36m# Static pages About page should have the content 'About Us'←[0m 

當訪問http://localhost:3000/static_pages/about頁面加載,我可以在大H1字母見「關於我們」。

規格/請求/ static_pages_spec.rb:

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 
    end 

    describe "Help page" do 

    it "should have the content 'Help'" do 
     visit '/static_pages/help' 
     expect(page).to have_content('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 
    end 
end 

配置/ routes.rb中:

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

應用程序/控制器/ static_pages_controller.rb:

class StaticPagesController < ApplicationController 
    def home 
    end 

    def help 
    end 

    def about 
    end 
end 

應用程序/視圖/ about.html.erb:

<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> 
+0

莫非你還要添加'Gemfile'和'app/views/layouts/application.html.erb'? – Mab879

回答

12

我注意到你錯過了最初的/在失敗的測試:

visit 'static_pages/about' 

VS

visit '/static_pages/help' 
+0

現貨,瘋狂分心! –

+0

不錯的一個,只是因爲完全相同的原因被困在確切的地方一個半小時。 Ughh – sivanes

相關問題