如何在Rails 5中存根current_user?

时间:2017-03-25 22:06:09

标签: ruby-on-rails ruby ruby-on-rails-5 minitest sorcery

如何在Rails 5中存根current_user

我期待这个:

@controller.stub :current_user, mock_user do

end

工作,但@controller中没有ActionDispatch::IntegrationTest?我使用sorcery gem进行身份验证。

1 个答案:

答案 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