0

I am trying to test routes added to a controller via a concern. I want to write acceptance tests that include the concern's routes in a controller in order to avoid regressions as well as document the concern's use cases. Does this make sense? If so, How would one go about accessing routes added to a controller via a concern in tests?

Following is a stripped down version of my app's routes. The permissionable concern routes work as expected when running the app.

#routes.rb
routes.draw do
  concern :permissionable do
    member do
      get 'permission', action: "permission_show"
     end
  end

  resources :profiles, concerns: :permissionable
end

and a stripped down version of my controller concern, which is included by my ProfilesController

module PermissionActions
  extend ActiveSupport::Concern

  def permission_show
    if current_user.admin?
      render :text => "you have permissions for this entity"
    end
    render :text => "No permission for this entity"
  end

   def test_a_thing
      "test"
  end
end

Finally, thanks to the answers in this question, my rspec examples look something like this.

describe ApplicationController, type: :controller do
  controller do
    include PermissionActions

    def index; end
  end

  describe 'a controller with permission actions' do
    it 'can call a method' do
      expect(@controller.test_a_thing()).to eq("test")
    end

    it 'exposes a permission route' do
      get :permission
    end
  end
end

The first example works, but the second example complains with No route matches {:action=>"permission", :controller=>"anonymous"}

I tried too many things to list all of them here, but the solution that I thought would work for sure was to re-draw the routes inside the test like this:

...
it 'exposes a permission route' do
  routes.draw {
    concern :permissionable do
      member do
        get 'permission', action: "permission_show"
      end
    end
    resources :anonymous, concerns: :permissionable
  }

  # response = @controller.permission_show()
  get :permission, :id => "anyId"

  expect(response).to have_http_status(200)
end
Marconius
  • 711
  • 1
  • 6
  • 15
  • 1
    have you tried `get :permission_show` in `it exposes a permission` – diabolist Aug 15 '17 at 11:38
  • @diabolist, that may have been the first thing I tried. – Marconius Aug 15 '17 at 12:05
  • @diabolist, while your suggestion did not work in the context of the conern spec, it _does_ work in the spec of a controller that includes the concern. so in my examples, `get :permission` does work in spec for `ProfilesController` ... so thanks for at least unblocking me. – Marconius Aug 16 '17 at 16:46
  • @diabolist, you were right! `:permission_show` is definitely the correct symbol to pass to `get`. The reason why it was not working for me originally was the way I was drawing the routes, where `:permissionable` exposes routes for `member` only. I don't know how to access the id of the anonymous controller (do you?), so instead I replaced `member` to `collection` and it worked. If you want to submit an answer, I could accept it. – Marconius Aug 16 '17 at 18:05
  • Glad I could help @Marconius. I don't think there is a need to submit an answer. – diabolist Aug 18 '17 at 19:55
  • Someone please add an answer so that other users can see that this question is no longer waiting for one. – zetetic Aug 19 '17 at 22:14

0 Answers0