How to test a Controller Concern in Rails 4

前端 未结 4 1413
孤独总比滥情好
孤独总比滥情好 2021-01-29 23:18

What is the best way to handle testing of concerns when used in Rails 4 controllers? Say I have a trivial concern Citations.

module Citations
    ex         


        
4条回答
  •  离开以前
    2021-01-29 23:42

    Simplifying on method 2 from the most voted answer.

    I prefer the anonymous controller supported in rspec http://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/anonymous-controller

    You will do:

    describe ApplicationController, type: :controller do
      controller do
        include MyControllerConcern
    
        def index; end
      end
    
      describe 'GET index' do
        it 'will work' do
          get :index
        end
      end
    end
    

    Note that you need to describe the ApplicationController and set the type in case this does not happen by default.

提交回复
热议问题