RSpec controller test: no route matches

Here's my msg error:

 1) VisitorSessionsController (1) Actions (a) #record params('view') has http success
     Failure/Error: expect ( get :record ).to have_http_status(:success)
     ActionController::UrlGenerationError:
       No route matches {:action=>"record", :controller=>"visitor_sessions"}

      

No route matches. I tried several ways to alleviate this error, but first take a look at the controller action:

  def record
    case params[:visitor_action]
      when 'view'
        impression = @visitor_session.impressions.create( ad_id: @ad.id )
        impression.start
      when 'click'
        @visitor_session.clicks.create( time: Time.now, ad_id: @ad.id )
    end
    render nothing: true
  end

      

Here are some of the ways I've tried to overcome the route issue:

 describe "params('view')" do 
    it "has http success" do
        #get :record, { :visitor_session => "view" }
        #expect(response).to have_http_status(:success)
        #expect ( get :record ).to have_http_status(:success)
    end
 end

      

However, they all return the same "no routing match" error. What's going on here that I am missing?

EDIT: Corresponding route:

get 'record/:visitor_action/:visitor_session_token/:ad_token', to: 'visitor_sessions#record', as: :record_action

      

+3


source to share


1 answer


You need to specify all the parameters defined in your route, or override the route to make them optional. The reason it can't match the route is because you don't provide :visitor_action

, :visitor_session_token

or :ad_token

in your request. You can make them optional, for example:



get 'record / (: visitor_action / (: visitor_session_token / (: ad_token)))', to: 'visitor_sessions # record', as :: record_action

+5


source







All Articles