I have a test that tries to view a subscription that doesn't exist. I run this test with a bunch of users from my fixtures. In the case of the user in the admin role when the app gets to the point of trying to render the response it has changed the action from :show
to :edit
and dropped the id
param. Yet when I try and use byebug to trace the execution I never seem to be able to pinpoint when it happens.
my test is:
test "#{u.role} can not view subscriptions that don't exist" do
self.send('sign_in_' + u.role)
get :show, id:1234567890
assert_redirected_to root_path
assert_includes flash[:alert], "That subscription doesn't exist"
end
where u
is a user loaded from my fixtures.
The error I get is:
SubscriptionsControllerTest#test_admin_can_not_view_subscriptions_that_don't_exist:
ActionView::Template::Error: No route matches {:action=>"edit", :controller=>"subscriptions", :id=>nil} missing required keys: [:id]
app/views/subscriptions/show.html.erb:13:in `_app_views_subscriptions_show_html_erb__1518678276755260966_70268849069860'
test/controllers/subscriptions_controller_test.rb:58:in `block (2 levels) in <class:SubscriptionsControllerTest>'
my controller looks like this:
class SubscriptionsController < ApplicationController
load_and_authorize_resource except: [:create,:new]
before_action :set_subscription
def show
end
def edit
end
...
private
def subscription_params
params.require(:subscription).permit(:email,:confirmed)
end
def set_subscription
#byebug if user_signed_in? && current_user.role == 'admin' && self.action_name == 'show'
begin
if (params.has_key? :id) && (controller_name == 'subscriptions')
@subscription = Subscription.find(params[:id])
elsif user_signed_in?
@subscription = current_user.subscription || Subscription.new(email: current_user.email)
else
@subscription = Subscription.new
end
rescue ActiveRecord::RecordNotFound
@subscription = Subscription.new
flash.alert = "That subscription doesn't exist"
end
end
end
load_and_authorize_resource
comes from cancancan.
my routes related to this test are:
resources :subscriptions do
member do
get 'confirm'
end
end
I don't really know where to go from here, so any advice would be appreciated.