如何在Rails 5中存根current_user
?
我期待这个:
@controller.stub :current_user, mock_user do
end
工作,但@controller
中没有ActionDispatch::IntegrationTest
?我使用sorcery
gem进行身份验证。
答案 0 :(得分:1)
您可以尝试wiki example:
class TeamsControllerTest < ActionController::TestCase
include Sorcery::TestHelpers::Rails::Integration
include Sorcery::TestHelpers::Rails::Controller
setup do
@team = teams(:one)
@user = users(:one)
login_user(user = @user, route = login_url) # replace with your login url path
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:teams)
end
这会将您的测试用户设置为current_user
。