如何通过minitest测试after_sign_in_path_for

问题描述 投票:2回答:1

我改变了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)?

如何通过q​​azxswpoi为after_sign_in_path_for编写测试?

Rails:5.1 devise:4.5.0

minitest
ruby-on-rails devise minitest
1个回答
0
投票

您可以像这样测试它们:

require 'test_helper'

class ApplicationControllerTest < ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers
  setup do
    2.times{ create(:post) }
    @user = create(:user)
    @admin_user = create(:admin_user)
  end

  test "should redirect to '/posts/1' after login" do
    # get "/posts/1"
    # sign_in(@user)
    # test return back "/posts/1"
  end

  test "should redirect to '/posts/2' after login" do
    # get "/posts/2"
    # sign_in(@user)
    # test return back "/posts/2"
  end

  test "should redirect to admin root page after login" do
    # sign_in(@adminuser)
    # test go to admin root page
  end
end

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

© www.soinside.com 2019 - 2024. All rights reserved.