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