2011-01-06 29 views
2

Ich versuche immer noch herauszufinden, rspec und im Moment benutze ich authlogic, um meine Benutzer und Sitzungen zu behandeln. Ich habe die üblichen Authlogic-Sachen wie das Hinzufügen der Def current_user-Methode zum application-Controller und als Hilfsmethode, aber wie greife ich darauf in meiner rspec-Controller-Datei?Wie überprüft man, ob die Hilfsmethode/Variable in rspec existiert?

Hier ist meine user_sessions_controller_spec.rb:

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 

wenn ich rspec führen Sie es sagt mir nur: i

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> 

so im es im rspec Kontext erraten ... aber wie weiß es sollte in user-sessions_controller sein? Und teste ich es sogar richtig?

Antwort

3

Insted von current_user in Ihrem RSpec, versuchen controller.current_user

Wenn Sie möchten, verwenden current_user Methode Stub: controller.stub!(:current_user).and_return(whatever) in before :each Block, aber dann werden Sie immer whatever, wenn man es Stummel.

Verwandte Themen