title: Rspec no route matches, checking for flash message in controller
body: <p>I need to test that a flash message comes up when a specific exception is raised. I am getting a no route matches error and have looked through the other topics with "Rspec Controller test, No Routes matches" and am still not sure what is wrong. This is my first time writing an rspec test. Commented out lines are various other attempts to get test to pass.</p>
  
  <p>audit/businesses_controller.rb: </p>
  
  <pre><code>class Audit::BusinessesController &lt; ApplicationController    
  # PUT /audit/businesses/:id
  def update
    remove_deleted_account_numbers_in(params)
    @business = @jurisdiction.businesses.find(params[:id])
    @business.changed_by = current_user
  
  if @business.update_attributes(audit_business_params)
    success_message('update')
  
  else
    @business.initialize_accounts
    @business = @business
    render :edit, alert: t('flash.save_failed')
  end 
  rescue ActiveRecord::StatementInvalid =&gt; e
    if e.message.include? "foreign key constraint"
      @business = @jurisdiction.businesses.find(params[:id])
      @business.initialize_accounts
      @business = @business
    render :edit, alert: "This account number is in use and cannot be    deleted"
    else
      raise e
    end
  end
  </code></pre>
  
  <p>business_controller_spec.rb:</p>
  
  <pre><code>require "spec_helper"
  
  describe Audit::BusinessesController do
  
  describe "handling ActiveRecord::StatementInvalid" do
   before {delete, url = 'audit/jurisdictions/:jurisdiction_id/businesses/:id(.:format)'} 
   it "should rescue with a flash message" do
  #     /audit/jurisdictions/:jurisdiction_id/businesses/:id(.:format) 
  #      delete, businesses_id: '80776', jurisdiction_id: '795', format: 'html', id: "234769"  
     expect(page).to have_content("This account number is in use and cannot be deleted")
   end
   end
  end
  </code></pre>
  
  <p>rake routes:                                                                                        </p>
  
  <pre><code>audit_businesses GET       
  /audit/jurisdictions/:jurisdiction_id/businesses(.:format)                                                           
  
  audit/businesses#
  POST    /audit/jurisdictions/:jurisdiction_id/businesses(.:format)                                                           
  
  audit/businesses#create
  new_audit_business GET     
  /audit/jurisdictions/:jurisdiction_id/businesses/new(.:format)                                                        
  
  audit/businesses#new
    edit_audit_business GET    
  
  /audit/jurisdictions/:jurisdiction_id/businesses/:id/edit(.:format)                                                    
  audit/businesses#edit
  audit_business 
  GET    /audit/jurisdictions/:jurisdiction_id/businesses/:id(.:format)                                                         
  audit/businesses#show
  PUT    /audit/jurisdictions/:jurisdiction_id/businesses/:id(.:format)                                                       audit/businesses#update
  DELETE /audit/jurisdictions/:jurisdiction_id/businesses/:id(.:format)                                                       audit/businesses#destroy
  </code></pre>
  
  <p>Error Message:</p>
  
  <pre><code>Failure/Error: before { delete audit_business_path(:jurisdiction_id, :id, :format) }
  ActionController::RoutingError:
     No route matches {:controller=&gt;"audit/businesses", :action=&gt;"/audit/jurisdictions/jurisdiction_id/businesses/id.format"}
   # ./spec/controllers/businesses_controller_spec.rb:6:in `block (3 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>When I change the spec to:</p>
  
  <pre><code>describe Audit::BusinessesController do
  
   describe "handling ActiveRecord::StatementInvalid" do
     it "should rescue with a flash message" do
       put :update, :jurisdiction_id =&gt; 1, :id =&gt; 1, :format =&gt; html
       expect(controller).to set_flash[:alert].to(/This account number is in use and cannot be deleted/).now
  </code></pre>
  
  <p>The error is: </p>
  
  <pre><code>Failure/Error: put :update, :jurisdiction_id =&gt; 1, :id =&gt; 1, :format =&gt; html
   NoMethodError:
     undefined method `can_update?' for nil:NilClass
  </code></pre>
  
  <p>routes.rb:</p>
  
  <pre><code>  namespace :audit do
  root to: 'site#home'
  
  resources :labels,        only: ['index', 'destroy'] do
    post 'delete_queue', on: :collection
    get 'print',         on: :collection
    get 'print_view',    on: :collection
    get 'mark_printed',  on: :collection
    get 'users',         on: :collection
  end
  resources :auditor_goals
  resources :contracts
  resource  :contract_work, only: %w[show update], controller: 'contract_work'
  resources :manpower
  
  resource :county_budgets, only: :update do
    get 'search'
  end
  
  get  'intro_letter_search' =&gt; 'files#intro_letter_search'
  put  'intro_letter_update' =&gt; 'files#intro_letter_update'
  get  'search'           =&gt; 'files#search'
  get  'calendar'         =&gt; 'files#calendar'
  get  'autocomplete'     =&gt; 'files#autocomplete'
  get  'direct'           =&gt; 'files#direct'
  
  get  'size_code_select' =&gt; 'businesses#size_code_select'
  
  scope '/jurisdictions/:jurisdiction_id' do
    resources :businesses, controller: 'businesses'
    resources :files,      controller: 'files' do 
      get 'print',         on: :member
    end
      # get 'page/:page', action: :index, on: :collection
  end
  end
  </code></pre>