Anonymous controller in Minitest w/ Rails

后端 未结 2 1948
暗喜
暗喜 2021-01-06 17:26

While converting from RSpec to Minitest I ran into a slight issue that Google has not helped with one bit, and that\'s figuring out how to do something like this:

         


        
2条回答
  •  甜味超标
    2021-01-06 17:32

    You can do something like that:

    # Add at runtime an action to ApplicationController
    ApplicationController.class_eval do
      def any_action
        render :nothing
      end
    end
    
    # If disable_clear_and_finalize is set to true, Rails will not clear other routes when calling again the draw method. Look at the source code at: http://apidock.com/rails/v4.0.2/ActionDispatch/Routing/RouteSet/draw
    Rails.application.routes.disable_clear_and_finalize = true
    
    # Create a new route for our new action
    Rails.application.routes.draw do
      get 'any_action' => 'application#any_action'
    end
    
    # Test
    class ApplicationControllerTest < ActionController::TestCase
      should 'do something' do
        get :any_action
    
        assert 'something'
      end
    end
    

提交回复
热议问题