我正在通過railstutorial.org的書第9章進行工作。運行測試套件時,我無法擺脫以下故障。所有的代碼都是通過首先輸入,然後從書中複製粘貼來嘗試的。Ruby on Rails教程測試未通過
...............F............................................
Failures:
1) SessionsController DELETE 'destroy' should sign a user out
Failure/Error: controller.should_not be_signed_in
expected signed_in? to return false, got true
# ./spec/controllers/sessions_controller_spec.rb:69:in `block (3 levels) in <top (required)>'
Finished in 8.28 seconds
60 examples, 1 failure
下面是用於測試在sessions_controller_spec.rb的代碼:
def destroy
sign_out
redirect_to root_path
end
的SIGN_OUT方法在發現:本(我認爲)sessions_controller.rb的相關部分
describe "DELETE 'destroy'" do
it "should sign a user out" do
test_sign_in(Factory(:user))
delete :destroy
controller.should_not be_signed_in
response.should redirect_to(root_path)
end
end
sessions_helper.rb
def current_user=(user)
@current_user ||= user_from_remember_token
end
def current_user
@current_user
end
def signed_in?
!current_user.nil?
end
def sign_out
cookies.delete(:remember_token)
self.current_user = nil
end
因此,如果我正確地理解了這一點,那麼在登錄工廠用戶之後,測試正在調用SessionsController的destroy方法,該方法調用sign_out(來自SessionsHelper),該方法明確將self.current_user設置爲nil。然後測試檢查signed_in?與行be_signed_in。由於代碼將self.current_user設置爲零,current_user.nil?應該返回true,!current_user.nil?應該返回false,這是測試想要的(controller.should_not be_signed_in)。
任何幫助,將不勝感激。我同時學習Ruby,Rails和TDD,所以我不確定我的問題在哪裏。
您可以添加如何實現current_user = function的代碼嗎? – 2011-02-07 03:41:07
current_user =(user)返回@current_user || = user_from_remember_token,並且在查看時發現了另一個錯誤,因爲current_user的定義方式與檢查本書的方式相同,應返回@current_user。但是,我改變了這一點,我仍然從測試中得到相同的錯誤。 – Chuck 2011-02-07 03:59:26