2

I tried to follow this answer: https://stackoverflow.com/a/32168734/148844

  # Logs in a test user.
  def log_in_as(user, options = {})
    password    = options[:password]    || 'password'
    remember_me = options[:remember_me] || '1'
    if integration_test?
      post user_session_path, session: { email: user.email,
                                  password:    password,
                                  remember_me: remember_me }

But I got this warning:

DEPRECATION WARNING: Using positional arguments in integration tests has been deprecated,
in favor of keyword arguments, and will be removed in Rails 5.1.

Deprecated style:
get "/profile", { id: 1 }, { "X-Extra-Header" => "123" }

New keyword style:
get "/profile", params: { id: 1 }, headers: { "X-Extra-Header" => "123" }
 (called from log_in_as at C:/Users/Chloe/workspace/fortuneempire/test/test_helper.rb:16)

So I tried

  post action: user_session_path, session: { email: user.email,
                              password:    password,
                              remember_me: remember_me }

and it gave

URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80{:action=>"/users/sign_in", :session=>{:email=>"MyString@example.com", :password=>"password", :remember_me=>"1"}}
    test/test_helper.rb:16:in `log_in_as'

I honestly can't see anything wrong with it. http://api.rubyonrails.org/classes/ActionController/TestCase/Behavior.html#method-i-get

This questions solution was to label the header parameters, but I have no such hash.

Rails 5.0.2

Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
Chloe
  • 25,162
  • 40
  • 190
  • 357

1 Answers1

1
post user_session_path, session: { email:       user.email,
                                   password:    password,
                                   remember_me: remember_me }

becomes

post user_session_path, params: { session: { email:       user.email,
                                             password:    password,
                                             remember_me: remember_me } }
Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
  • Are you sure? The docs says `session:` is a separate key, not a member of `params:`. – Chloe Jun 15 '17 at 20:10
  • This is how I fixed it on my side but after researching it seems to be a coincidence that your parameter is named session as this message comes from a [different class](https://github.com/rails/rails/blob/5-0-stable/actionpack/lib/action_dispatch/testing/integration.rb#L375) than the one you were looking at. As you can see [people on github usually name it "user"](https://github.com/search?l=Ruby&q=%22post+user_session_path%22&type=Code&utf8=%E2%9C%93) rather than "session", hence the confusion – Stéphane Bruckert Jun 15 '17 at 22:05
  • Oh I think I see. I am using [Devise](https://github.com/plataformatec/devise) for authentication and it creates `new_user_session` paths. I thought it was setting the user's Session/Cookies instance. – Chloe Jun 18 '17 at 19:28
  • Another subtlety here is that `get user_session_path` will trigger the deprecation warning, even though there's (seemingly) no old-style positional parameters provided at all. Add empty params anyway, e.g. `get user_session_path, params: {}` – DreadPirateShawn Jan 30 '21 at 07:12