user5292389
user5292389

Reputation:

Minitest IntegrationTest ArgumentError: unknown keyword: session

I implement an integration test using minitest like that

test "should get index" do
  get admin_product_url, params: {}, session: { user_id: @admin_user.id }
  assert_response :success
end

but I get this error message

ArgumentError: unknown keyword: session

how else can I set session information?

I am using ruby 2.4 and rails 5.0.2

Upvotes: 5

Views: 2178

Answers (3)

Chris Kottom
Chris Kottom

Reputation: 1289

Integration tests approximate a browsing session, so the requests you make can only use pass parameters, headers, etc. From that perspective, it makes sense that the session accessor only becomes available once you've made an initial request and that you can't pass a Hash of session variables like this. Ideally, you should be testing more complex interactions, not just single requests, and you should be making assertions about the session values that your controller logic is setting. But you could structure the test above like this:

test "should get index" do
  get admin_product_url                  # first request initializes session
  session[:user_id] = @admin_user.id     # can now update session state
  get admin_product_url                  # subsequent request
  assert_response :success               # assertions follow
end

Upvotes: 2

Roman Kiselenko
Roman Kiselenko

Reputation: 44370

In Rails 5 is no longer possible to access session in controller tests. Integration tests doesn't allow you to set session.

Ref

Upvotes: 3

NM Pennypacker
NM Pennypacker

Reputation: 6952

This should work:

test "should get index" do
  session[:user_id] = @admin_user.id
  get admin_product_url, params: {}
  assert_response :success
end

Upvotes: 0

Related Questions