Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to test helpers in Rails with Devise current_user and ActionView::TestCase Discussion

Chris Oliver asked in General

Thanks Chris!
Update: Rubocop recommend to use (Style/TrivialAccessors: Use attr_reader to define trivial reader methods.)

attr_reader :current_user

Reply

@Chris, I was trying to go through an old version of Rail Tutorial (probably Rails 4) and ran into a similar issue, while creating the authentication system it seems like you said here, we need to define the current_user, logged_in? and other methods again for the tests to work. I finally however, gave up when I realized that I got the error below. It seems counter intuitive though that methods (even helper_methods) defined in the controller cannot be accessed by the TestController, is there any specific reason why this was done?

test_current_user_returns_right_user_when_session_is_nil#ApplicationControllerTest (0.07s)
Minitest::UnexpectedError: NoMethodError: undefined method `session' for nil:NilClass

This is due the usage of session[user_id] in the current_user method which I did include in the ControllerTest

Reply
Join the discussion
Create an account Log in

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

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

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

    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.