代码之家  ›  专栏  ›  技术社区  ›  ironsand

如何通过minitest在路径中签名后进行测试

  •  0
  • ironsand  · 技术社区  · 5 年前

    我改变了 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 ,我不能重写 最小的 .

    How to test after_sign_in_path_for(resource)?

    我怎么写测试 在路径中签名后 通过 最小的 ?

    钢轨:5.1 设计:4.5.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
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   Veridian Dynamics    5 年前

    你可以这样测试他们:

    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