2011-01-06 96 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如果你存根它。

相关问题