我改变了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)?
如何通过qazxswpoi为after_sign_in_path_for
编写测试?
Rails:5.1 devise:4.5.0
minitest
您可以像这样测试它们:
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