2011-01-06 81 views
2

我仍然試圖找出rspec,現在我正在使用authlogic來處理我的用戶和會話。我做了通常的authlogic,比如將def current_user方法添加到applciation控制器,並作爲輔助方法,但是如何在我的rspec控制器文件中訪問該方法?如何檢查rspec中是否存在輔助方法/變量?

這裏是我的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 

當我運行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的背景下...但我怎麼知道它應該在用戶會話控制器?我是否正確地測試它?在你的RSpec CURRENT_USER的

回答

3

insted的,儘量controller.current_user

如果你想存根CURRENT_USER方法使用:controller.stub!(:current_user).and_return(whatever)before :each 塊,但你總是會得到whatever如果你存根它。

相關問題