vendredi 15 février 2019

Rspec;How to include params inside the body

Rspec spec fails on the following test case. Can anyone please help me with sending params inside the body?

Test case:

  describe 'POST search' do
    it 'renders search' do
      request.headers['Content-Type'] = 'application/json'
      request.headers['Accept'] = 'application/json'

      post :search, body: {name: {query_type: 'match', value: ['xy']} }.to_json

      expect(response.status).to eq(200)
      expect(response.body.include?('TWEEDLE'))
    end
  end

Controller:

def search
    page_params = pagination_params

    query_hash = QueryPreprocessor.params_to_query_with_types(JSON.parse(request.body.read).deep_symbolize_keys)
    logger.info "query_hash: #{query_hash}"
    es_query_json = Elastic::QueryBuilder.facility_search_v1(query_hash, page_params).to_json
    logger.info "es query: #{es_query_json}"

    facilities = facility_helper.search es_query_json
    json_response build_facilities_response(facilities)
  rescue ApiError => e
    render json: e.response, status: e.status
  end

and these are the params which I want to include inside the body, which is being called in the controller:

def pagination_params
    page_params = {}
    page_params['size_params'] = params[:size] || 50
    page_params['from_params'] = params[:from] || 0
    page_params['sort_params'] = params[:sort]
    page_params['order_params'] = params[:order]
    page_params
  end

Rspec passes, with pagination params included inside the body.

Aucun commentaire:

Enregistrer un commentaire