2010-09-27 70 views
5

測試我有用於測試控制器如下用戶登錄而使用RSpec和authlogic

require 'spec_helper' 

describe ProductsController do 
setup :activate_authlogic 

describe "user not logged in" do 

it "should not GET index" do 
get :index 
response.should redirect_to(login_path) 
end 

end 

describe "user logged in" do 

before(:each) do 
UserSession.create :username => "rohit", :password => "test123" 
end 

it "should GET index" do 
get :index 
response.should redirect_to(products_path) 
end 

end 

end 

一個規範我也使用這條線在spec_helper.rb

require "authlogic/testcase" 

的測試爲「用戶沒有登錄通行證「,但」用戶登錄「失敗

'ProductsController user is logged in should GET index' FAILED 
expected redirect to "/products", got no redirect 

回答

-1

這似乎很正常,因爲你拿'/產品與登錄用戶的網址。然後他看到這個頁面。他沒有重定向到他看到的頁面。

每個測試都是獨立的。在以前的測試中沒有狀態保存。

+0

爲什麼我在「用戶登錄」示例中得到一個不重定向,至少它應該重定向到某處。我甚至試圖使用redirect_to(login_path)而不是redirect_to(products_path),但我仍然得到相同的錯誤。這是爲什麼? – Rohit 2010-09-27 12:07:30

+0

,因爲如果在這個控制器中沒有redirect_to其他東西,那麼你只會詢問頁面'/ products',這是正常的。把你的生產控制器,如果你想要更多的信息 – shingara 2010-09-27 12:12:15

+0

所以,基本上你在說什麼。我應該從規格 – Rohit 2010-09-27 12:17:13