New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow component tests to define the current_user #1212
base: main
Are you sure you want to change the base?
Conversation
| def with_current_user(user) | ||
| controller.define_singleton_method(:current_user) { user } | ||
| yield | ||
| ensure | ||
| controller.define_singleton_method(:current_user) { nil } | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced this is the best approach, but I'm haven't been able to think of other approaches yet that would work and don't depend on a particular testing framework.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this is a great application-level helper, but I'm not sure how it fits into the framework. In the GitHub codebase we've defined our own custom method for this that is specific to our application.
Do you know if/how authentication library helper methods like Devise's sign_in helper work with ViewComponent? I think that could provide some guidance on how to move forward here.
| @@ -3,6 +3,8 @@ | |||
| class IntegrationExamplesController < ActionController::Base | |||
| layout false | |||
|
|
|||
| helper_method :current_user | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assumed the default test controller was an ok place to define this, and I didn't want to obfuscate the intention of the test by creating a specific controller.
Authentication gems like [clearance] and [devise] define a `current_user` helper method that can be used in controllers and views to access the currently signed in user. When building components that depend on the currently signed in user and delegating `:current_user` to the Rails helpers, it is useful to be able to define `current_user` in tests. The `with_current_user` helper method defines a current_user method on the controller so that these components can be tested easily. [clearance]: https://github.com/thoughtbot/clearance [devise]: https://github.com/heartcombo/devise
a11c8bf
to
0aaf2b2
Compare
|
@cpjmcquillan doesn't devise already have a |
|
Sorry I haven't found time to pick this up again yet. Devise and Clearance both have test helpers that differ slightly in implementation. I think it would be nice if a ViewComponent test helper was authentication-gem-agnostic, but I'm not sure if that is achievable. Ultimately some guidance in the docs with clear examples for common libraries might be sufficient. I'll try and pick this up in the next couple of weeks. |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Summary
Authentication gems like clearance and devise define a
current_userhelpermethod that can be used in controllers and views to access the currently signed
in user.
When building components that depend on the currently signed in user and
delegating
:current_userto the Rails helpers, it is useful to be able todefine
current_userin tests.The
with_current_userhelper method defines a current_user method on thecontroller so that these components can be tested easily.
Other Information
Discussion a while back indicated there could be appetite for a test helper like this
in
ViewComponent::TestHelpers.