2011-01-06 4 views
2

まだrspecを把握しようとしていますが、今はauthlogicを使ってユーザーとセッションを処理しています。私は、applciationコントローラにdef current_userメソッドを追加するような、そしてヘルパーメソッドとして、通常のauthlogicのやり方をしましたが、どうすれば私のrspecコントローラファイルにアクセスできますか?ここでヘルパーメソッド/変数がrspecに存在するかどうかを確認する方法?

は私user_sessions_controller_spec.rbですので、それはRSpecのコンテキストにある推測イム

correct authorization should create a new session 
undefined local variable or method `current_user' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_1:0x000001017d3410> 

...しかし、どのようにしてください:私はRSpecの実行時に

require 'spec_helper' 
require 'authlogic' 

describe UserSessionsController do 

    context "user is already logged in" do 

    before(:each) do 
     include Authlogic::TestCase 
     activate_authlogic 
     UserSession.create Factory.build(:user) 
    end 

    it "should redirect the user to the home page" do 
     get 'new' 
     response.should redirect_to(home_path) 
    end 

    end 

    describe "#create" do 
    context "when the user is not logged in" do  
     before(:each) do 
     current_user = nil 
     end 

     it "correct authorization should create a new session" do 
     post 'create', {:login => "afactoryuser", :password => "apass", :password_confirmation => "apass"} 
     current_user.should_not be_nil 
     end 
    end 
    end 

end 

それはちょうど私に語りましたuser-sessions_controllerにあるはずですか?私はそれを正しくテストしていますか?あなたはCURRENT_USERメソッドの使用をスタブにしたい場合は

答えて

3

あなたのRSpecの中にCURRENT_USERのInsted、controller.current_user

を試してみてください。controller.stub!(:current_user).and_return(whatever)ブロック before :eachに、しかし、あなたはそれをスタブならば、あなたは常にwhateverを取得します。

関連する問題