2017-03-09 6 views
0

매개 변수 비교가있는 테스트가 하나 있습니다. In rails 5 params 이미 객체와이 객체의 매개 변수에있는 각각의 새로운 해시. 이 매개 변수를 어떻게 비교할 수 있습니까?매개 변수 비교 ActionController :: 테스트의 매개 변수

이 오류가 발생합니다.

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 
+0

컨트롤러 코드를 표시하십시오. – margo

+0

@margo 님이 질문을 수정했습니다. –

답변

0

테스트를 수정했는데, 어쩌면 내가 잘못했는지, 뭐라고 말합니까?

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