2,260 Experience
22 Lessons Completed
0 Questions Solved


Thanks Chris!

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?


Currently I am using the following setup:

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

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

def payload_for_jwt
    source: controller.request.original_url

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.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

© 2024 GoRails, LLC. All rights reserved.