[英]How to Test The Current User using RSPEC & Here is my controller
This is the AdminController Which is checking the current account and current Role of the user using session in the first action and in the second action we are checking the current role of current user as there are multiple roles for each user.这是 AdminController,它在第一个操作中使用会话检查用户的当前帐户和当前角色,在第二个操作中我们正在检查当前用户的当前角色,因为每个用户都有多个角色。
class AdminController < ActionController::Base
protect_from_forgery
skip_before_action :verify_authenticity_token
def current_account
if session[:current_account_id].present?
Account.find(session[:current_account_id])
end
end
def current_role
if current_account.present?
current_account.account_users.find_by(user: current_user).admin_role
else
AccountUsers::DEFAULT_USER_ROLE
end
end
end
You can create an ordinary user with FactoryBotRails您可以使用 FactoryBotRails 创建普通用户
let(:user) { create :user }
and stub current user like this for example例如,像这样存根当前用户
allow_any_instance_of(AdminController).to receive(:current_user).and_return(user)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.