Ask A Question

Notifications

You’re not receiving notifications from this thread.

Access the request object in a helper in a helper test (minitest)

Wout asked in Testing

Using Rails 6 and minitest, I am testing a helper method which generates a JWT token to make a request to an external service. Inside the JWT's payload hash, I need the pass the source url. Ideally, I'd like to use request.original_url, but the request object is not available when the helper test runs.

How can I mock the request object so that it's accessible in the helper at the moment the test runs?

Update:

Currently I am using the following setup:

setup do
  controller.request = ActionDispatch::Request.new(:test)
end

And similarly accessing the request object on the controller insite my helper method:

def payload_for_jwt
  {
    source: controller.request.original_url
  }
end

It works, but is it the correct approach? Cleaner would be if the request object can be accessed directly, but that's a minor issue I thnk.

Reply
Reply

Thanks Chris!

Reply
Join the discussion
Create an account Log in

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

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

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