@current_user
你的测试是零。
你的问题就在这里。
allow_any_instance_of(ApplicationController).to receive(:set_current_user).and_return(fuser)
set_current_user
实际上并不返回用户对象,而是将一个对象分配给
@当前_用户
变量,然后可能重定向。
更重要的是,rails会以这种方式设置用户:
class ApplicationController < ActionController::Base
before_action :verify_current_user!
def current_user
@current_user || User.find_by_session_token(cookies[:session_token])
end
def verify_current_user!
redirect_to login_path unless current_user
end
end
然后,在引用当前登录的用户时,调用
current_user
方法该值将被记忆,因此没有性能损失。您还可以将
当前_用户
方法。在控制器中,始终调用
当前_用户
而不是
@当前_用户
.