2011-02-07 19 views
0

私はrailstutorial.orgの第9章で作業しています。テストスイートを実行すると、次のエラーを取り除くことはできません。すべてのコードは、最初にそれを入力し、次に本からコピーして貼り付けることによって試行されています。Ruby on Railsチュートリアルテストが合格しない

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_controller.rbの関連部分:

def destroy 
    sign_out 
    redirect_to root_path 
end 

sign_out方法が中に発見された。ここ

...............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でテスト用のコードです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メソッドが呼び出されます。このメソッドは、self_current_userを明示的にnilに設定しているsign_out(SessionsHelperから)を呼び出します。テストではsigned_inをチェックしますか?ラインはbe_signed_inである。コードはself.current_userをnilに設定するので、current_user.nil? trueを返すべきです、!current_user.nil? falseが返されます。これはテストが望むものです(controller.should_not be_signed_in)。

ご協力いただければ幸いです。 Ruby、Rails、TDDを同時に学習しているので、どこに問題があるのか​​分かりません。

+0

current_user =関数の実装方法のコードを追加できますか? –

+0

current_user =(user)は@current_user || = user_from_remember_tokenを返し、別の間違いを見つけたとき、current_userが同じ方法で定義されているので、本をチェックすると@current_userだけが返されます。しかし、私はそれを変更し、私はまだテストから同じエラーが発生しています。 – Chuck

+0

私は申し訳ありません、signed_in? sessions_helper.rbにあります。これを反映するために質問を編集し、current_user =とcurrent_userのコードを含めます。 – Chuck

答えて

2

問題は現在のユーザーを設定する方法です。覚えているトークンからログインするように設定しているだけで、これをnilに設定することはできません。次のように試してみるとよいでしょう:

 
def current_user=(user) 
    @current_user = user 
end 

def current_user 
    @current_user ||= user_from_remember_token 
end 
+0

ありがとうございました。 – Chuck