如何通过minitest测试after_sign_in_path_for

时间:2019-03-14 07:24:32

标签: ruby-on-rails devise minitest

我更改了after_sign_in_path_for方法的默认行为,如下所示:

class ApplicationController < ActionController::Base
  private
  def after_sign_in_path_for(resource)
    return admin_root_path if resource.is_a?(AdminUser)
    request.referrer || root_path
  end
end

它可以找到,现在我要通过minitest对其进行测试。 但是我不知道该如何编写集成测试。

尽管rspec有一个答案,但我不会为minitest重写。

How to test after_sign_in_path_for(resource)?

如何为after_sign_in_path_for的{​​{1}}编写测试?

路轨:5.1 设计:4.5.0

minitest

1 个答案:

答案 0 :(得分:0)

您可以像这样测试它们:

require 'test_helper'

class ApplicationControllerTest < ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers
  setup do
    @user = create(:user)
    @admin_user = create(:admin_user)
  end

  test "should redirect to current page after login" do
    sign_in(@user)
    get :index
    assert_redirected_to controller: "home", action: "index"
  end

  test "should redirect to admin root page after login" do
    sign_in(@adminuser)
    get :index
    assert_redirected_to controller: "admin", action: "index"
  end
end

assert_redirected_to API docs