0

The following test_helper method, given Devise as the authentication method,

  def sign_in_with_section_id_as(user)
    post user_session_url, params: {  user_id: user.id, selected_section: 31 }
  end

is not setting the context for a test where a session[:selected_section] value needs to be processed, using the technique suggested here. However, when run,

      sign_in_with_section_id_as(roleshopuser.user)
puts session[:selected_section]

that context is returning nil for the session value.

How can this be achieved, possibly with the the ActionDispatch Integration methods relating to sessions ?

Jerome
  • 5,583
  • 3
  • 33
  • 76
  • How did you include `selected_section` in your (User) model and controller? Have you included `Devise::Test::IntegrationHelpers` in your test file? See [Devise Reference re Test helpers](https://github.com/heartcombo/devise#test-helpers). – Masa Sakano Dec 06 '21 at 12:15
  • Yes, the Devise Integration helpers are included. `session[:selected_section] = @section.id` is set in a previous action. – Jerome Dec 06 '21 at 12:16
  • I think the fact the session value returns `nil` implies the value is not set as you intend. Presenting the relevant code to set it in your question may help? Also, is [this answer](https://stackoverflow.com/a/7422987/3577922) relevant, or not? – Masa Sakano Dec 06 '21 at 12:28
  • in the application the session value is set in another action and that is tested and successfully verified (with that above statement). In this test, it is set to a static integer value for semplification purposes. so it is the test statement that is inadequate. And, no, that reference relates to setting session values for the devise resource; here we're not dealing with the application, but just the test. – Jerome Dec 06 '21 at 16:26
  • Is your question about an integrated test? I thought it was about a unit test of a Controller, where `session[XXX]` is nil in default (that is why I suggested that you present the relevant code in which you set the session variable). If your question is about an integrated test that fails against your expectation, I think a lot more information about your test is essential for anyone to answer your question… As a reminder, `params` never sets `session` automatically in Rails. – Masa Sakano Dec 06 '21 at 20:19

0 Answers0