0

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.

Camden Narzt
  • 2,271
  • 1
  • 23
  • 42

2 Answers2

1

Take a look at the stack trace for this exception:

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>'

app/views/subscriptions/show.html.erb, line 13. Are you calling link_to (or a similar helper method) with a nil ID, maybe?

0

Look at your error message. It says the id parameter is missing. It is possible you are giving it a nil value. Because of this the router can't route the request properly.

Also, the error is for an request to the edit action, but the code you are showing is calling the show action. Can you clean up the code examples and error messages shown and make them consistent?

blowmage
  • 8,854
  • 2
  • 35
  • 40
  • Actually that's what's confusing me, I am getting an error for not providing an id to the edit route when I'm obviously doing a GET to show with an id, and byebug confirms this, yet I still get the error. – Camden Narzt Jun 29 '15 at 19:52
  • 1
    You have a link on line 13 of the `app/views/subscriptions/show.html.erb` template that is trying to link to the edit URL, but doesn't have an `id` value. e.g. `edit_subscription_path` vs. `edit_subscription_path(@subscription)` – blowmage Jul 02 '15 at 21:37