Ask A Question

Notifications

You’re not receiving notifications from this thread.

Rails 7.1 Authentication From Scratch - Test Helpers

Julian Keenaghan asked in Testing

I've implemented the Rails authentication in my 7.1 app but was wondering how to go about stubbing the current_user or session in my RSpec Request specs.

I'm normally used to have a Devise or Clearance helper that does this.

Anyone know how to do this?

Reply

For controller & integration tests, you'll make a POST request to log the user in:

class ActiveSupport::TestCase
  def login(user, password: "password")
    post login_path, params: {
      email: user.email,
      password: password
    }
  end
end
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.