我有麻烦讽刺应该从authlogic麻烦。从摩卡应用程序嘲笑authlogic
我有以下的测试夹具:
class HomeControllerTest < ActionController::TestCase
context "on GET to index" do
setup do
activate_authlogic
UserSession.stubs(:current_user).returns(user_session)
get :index
end
should respond_with :success
end
def user_session
@user_session.stubs(:user).returns(Factory.build(:user))
@user_session.stubs(:record)
@user_session
end
end
的问题是,在ApplicationController中的require_user方法仍然没有得到CURRENT_USER。
def current_user
puts "#{defined?(@current_user)}"
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
def require_user
unless current_user
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to login_url
return false
end
end
任何人都可以看到什么是错的?
current_user方法的最后一行直接出自AuthLogic。在Ruby中,像'result = foo && foo.bar'这样的行对于“将变量结果的值设置为foo对象的bar属性,但只有在设置了foo变量时才是方便的简写”。如果foo没有设置,'foo && foo.bar'会返回'nil'。如果设置了foo,则返回foo.bar的值。 – Chris 2010-09-07 11:12:51
不够公平,但我仍然认为将其扩展到更多行可以更容易地弄清楚测试中出现了什么问题。我已经更新了我的答案,以消除不正确的返回值假设。 – Shadwell 2010-09-07 11:14:33
另外,'current_user'方法的返回值不是'current_user_session.record'的值吗?如果某些事情出现问题,那么可能是“无”。 – Shadwell 2010-09-07 11:20:07