Skip to content

Support query parameters#33

Open
sanfrecce-osaka wants to merge 1 commit intor7kamura:mainfrom
sanfrecce-osaka:support-query-parameters
Open

Support query parameters#33
sanfrecce-osaka wants to merge 1 commit intor7kamura:mainfrom
sanfrecce-osaka:support-query-parameters

Conversation

@sanfrecce-osaka
Copy link
Contributor

Motivation / Background

In request spec, even if you set query parameters to
params, we can set values to Controller's params.

describe ‘GET /users’ do
  let(:params) { status: ‘active’ }

  it ‘returns 200’ do
    # below is equal to `get ‘/users, params:`
    subject # On UsersController, params[:status] returns ‘active’
    expect(response).to have_http_status 200
  end
end

However, when application/json is set for Content-Type, RSpec::RequestDescriber converts params to a JSON string using to_json, so even if query parameters are set in params, the test will not work correctly.

describe ‘GET /users’ do
  before do
    headers[‘content-type’] = ‘application/json
  end

  let(:params) do
    { status: ‘active’ }
  end

  it ‘returns 200 do
    subject # On UsersController, params[:status] returns nil
    expect(response).to have_http_status 200
  end
end

In actual behavior, query parameters should be included in the path, and it is preferable to verify the actual behavior as much as possible in tests.

Details

I added query. It can be defined as Hash, similar to params. Defining as String is not supported.

I added `query`. It can be defined as Hash, similar to `params`. Defining as String is not supported.
@r7kamura
Copy link
Owner

r7kamura commented Jul 23, 2025

Introducing this change could cause issues if a #query method is already defined in the existentt test code, so I believe we need to approach this kind of change with caution. This gem is used in large applications with thousands or even tens of thousands of examples, so it's easy to imagine cases where a commonly named method like #query has already been defined there 🤔

@r7kamura
Copy link
Owner

By the way, in the example you gave, you're specifying a Content-Type header for a GET request—does that mean you're intending to send a request body with the GET request? That seems unusual to me, since it's generally not standard practice.

I’m aware that there are exceptions like Elasticsearch that require it, but in general, GET requests do not include a body. I’d be interested to know what kind of use case you had in mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants