参数ActionController ::参数在测试中的比较

时间:2017-03-09 14:19:19

标签: ruby-on-rails rspec ruby-on-rails-5 rspec-rails

有一个测试参数的比较。在rails 5中params已经是对象,并且每个新哈希都在这个对象的参数中。如何比较这些参数?

我收到此错误。

 Failure/Error: expect(session[:last_ticket_search]).to eq(params)

   expected: <ActionController::Parameters {"q"=>{"ticket_number_eq"=>"123-123-123"}, "advanced_search"=>"t", "selected_columns"=>["ticket_number", "our_service_areas"], "commit"=>"Apply"} permitted: false>
        got: {:q=><ActionController::Parameters {"ticket_number_eq"=>"123-123-123"} permitted: false>, :advanced_search=>"t", :selected_columns=>["ticket_number", "our_service_areas"], :commit=>"Apply"}

   (compared using ==)

   Diff:
   @@ -1,2 +1,5 @@
   -<ActionController::Parameters {"q"=>{"ticket_number_eq"=>"123-123-123"}, "advanced_search"=>"t", "selected_columns"=>["ticket_number", "our_service_areas"], "commit"=>"Apply"} permitted: false>
   +:advanced_search => "t",
   +:commit => "Apply",
   +:q => <ActionController::Parameters {"ticket_number_eq"=>"123-123-123"} permitted: false>,
   +:selected_columns => ["ticket_number", "our_service_areas"],

在这个测试中。

it 'will save last search params to session when saved advanced search is applied' do
  search = FactoryGirl.create(:search, account: @account)

  get :index, params: {advanced_search: 't', search_id: search.id, q: {ticket_number_eq: '123-123-123'}, selected_columns: ['ticket_number', 'our_service_areas']}

  params = {
    advanced_search: 't',
    search_id: search.id.to_s,
    selected_columns: ['ticket_number', 'our_service_areas'],
    q: {'ticket_number_eq' => '123-123-123'}
  }


  expect(session[:last_ticket_search]).to eq(params)
end

指数行动。

def index
  authorize Ticket

  @search = current_account.searches.find_by(id: params[:search_id])
  @autorefresh_interval = autorefresh('tickets')

  @q = policy_scope(Ticket).ransack(params[:q])

  if params[:q].blank? && params[:advanced_search].blank?
    @q.add_default_condition('status', 'in', 'open')
    session[:last_ticket_search] = nil
  else
    # This is required for Chrome to redirect back to the applied search.
    # Firefox and Edge act a bit different than Chrome and do that without this
    # when using the back link functionality in navigation.coffee.
    session[:last_ticket_search] = {q: params[:q]}
    if params[:advanced_search].present?
      session[:last_ticket_search].merge!(advanced_search: 't', selected_columns: params[:selected_columns])
      session[:last_ticket_search].merge!(params[:search_id].present? ? {search_id: params[:search_id]} : {commit: 'Apply'})
    end
  end

  @selected_columns = selected_columns
  @tickets = @q.result.select('tickets.*')
                      .includes(:account, :ticket_responses, :assignee, :tags)
                      .order('tickets.created_at DESC')
                      .group('tickets.id')
                      .paginate(page: params[:page], per_page: per_page('tickets'))
end

2 个答案:

答案 0 :(得分:1)

修正了测试,因为它发生了,也许我做错了,你说什么?

it 'will save last search params to session when advanced search is applied' do
  get :index, params: {advanced_search: 't', q: {ticket_number_eq: '123-123-123'}, selected_columns: ['ticket_number', 'our_service_areas']}

  params = {
    q: ActionController::Parameters.new('ticket_number_eq' => '123-123-123'),
    advanced_search: 't',
    selected_columns: ['ticket_number', 'our_service_areas'],
    commit: 'Apply'
  }

  expect(session[:last_ticket_search]).to eq(params)
end

答案 1 :(得分:0)

我认为您不需要在测试请求中提供params术语。尝试

get :index, advanced_search: 't', search_id: search.id, q: {ticket_number_eq: '123-123-123'}, selected_columns: ['ticket_number', 'our_service_areas']