RSpec控制器测试:没有路线匹配

时间:2014-12-08 16:05:24

标签: ruby-on-rails rspec

这是我的错误信息:

 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"}

没有路线匹配。我已经尝试了几种方法来缓解此错误,但首先,请看一下控制器操作:

  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

以下是我尝试克服路线匹配问题的几种方法:

 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

然而所有这些都返回相同的错误,"没有路线匹配"。我错过了什么?

编辑:相关路线:

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

1 个答案:

答案 0 :(得分:5)

您需要提供路线中定义的所有参数或重新定义路线以使其成为可选参数。它无法与路线匹配的原因是您未在请求中提供:visitor_action:visitor_session_token:ad_token。您可以将它们设为可选:

  

获取'记录/(:visitor_action /(:visitor_session_token /(:ad_token)))',以:' visitor_sessions#record',as :: record_action