题
我还在试图找出rspec的和现在我使用authlogic来处理我的用户会话。我做了通常的authlogic东西,如添加高清CURRENT_USER方法applciation控制器,并作为一个辅助方法,但我怎么访问,在我的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的背景下...但我怎么知道它应该在用户sessions_controller?和我我甚至正确的测试它?
解决方案
在您的RSpec CURRENT_USER的insted的,尽量controller.current_user
如果您想存根CURRENT_USER方法使用:在controller.stub!(:current_user).and_return(whatever)
before :each
块,但你总是会得到whatever
如果你存根它。
不隶属于 StackOverflow