已經由邁克爾·哈特 以下的Rails教程Mac OS X上軌3.0版本10.7Rails pages_controller_spec.rb測試不應該失敗,但是,錯誤?
$ rspec的投機/
......FF
Failures:
1) PagesController GET 'help' should be successful
Failure/Error: get 'help'
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>"help"}
# ./spec/controllers/pages_controller_spec.rb:45:in `block (3 levels) in <top (required)>'
2) PagesController GET 'help' should have the right title
Failure/Error: get 'help'
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>"help"}
# ./spec/controllers/pages_controller_spec.rb:49:in `block (3 levels) in <top (required)>'
Finished in 0.14686 seconds
8 examples, 2 failures
Failed examples:
rspec ./spec/controllers/pages_controller_spec.rb:44 # PagesController GET 'help' should be successful
rspec ./spec/controllers/pages_controller_spec.rb:48 # PagesController GET 'help' should have the right title
測試看起來是這樣的:
require 'spec_helper'
describe PagesController do
render_views
describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
it "should have the right title" do
get 'home'
response.should have_selector("title",
:content => "Ruby on Rails Tutorial Sample App | Home")
end
end
describe "GET 'contact'" do
it "should be successful" do
get 'contact'
response.should be_success
end
it "should have the right title" do
get 'contact'
response.should have_selector("title",
:content => "Ruby on Rails Tutorial Sample App | Contact")
end
end
describe "GET 'about'" do
it "should be successful" do
get 'about'
response.should be_success
end
it "should have the right title" do
get 'about'
response.should have_selector("title",
:content => "Ruby on Rails Tutorial Sample App | About")
end
end
describe "GET 'help'" do
it "should be successful" do
get 'help'
response.should be_success
end
it "should have the right title" do
get 'help'
response.should have_selector("title",
:content => "Ruby on Rails Tutorial Sample App | Help")
end
end
end
而且我有in pages_controller.rb
class PagesController < ApplicationController
def home
@title = "Home"
end
def contact
@title = "Contact"
end
def about
@title = "About"
end
def help
@title = "Help"
end
end
而在routes.rb中我有
SampleApp::Application.routes.draw do
get "pages/home"
get "pages/contact"
get "pages/about"
get "pages/help"
end
當然,我也創造了在應用程序/視圖/ 奇怪的是,當我運行軌道服務器並轉到本地主機頁面help.html.erb頁:3000/pages/help我用合適的標題獲得適當的頁面,使其看起來好像測試應該已經通過,但事實並非如此。此外,聯繫人,家庭和關於測試通過,但當我剛剛添加的幫助它不是因爲某些未知的原因。這真的讓我感到困擾,我忽略了這個簡單的解決方案,它讓我瘋了嗎?
出於某種原因,我help.html.erb文件是275個字節,而我的約,接觸和家庭.html.erb頁面都是2KB,儘管他們幾乎是準確的每個字符的數量相同。這肯定與help.html.erb在測試時不起作用有關。此外,幫助的文檔類型是TextMate文檔,而其他文檔只是說「文檔」。奇怪..? – Laser 2012-01-18 20:38:05
我認爲你在正確的軌道上 - 代碼中沒有任何東西看起來會失敗。我會嘗試刪除並重新創建help.html.erb文件。 – 2012-01-18 20:39:50
感謝您的想法,不幸的是它沒有幫助。特別奇怪的是,它給我一個錯誤,耙路線是矛盾的,因爲在這裏解釋:http://stackoverflow.com/questions/8917201/my-computer-hates-me – Laser 2012-01-18 20:55:36