Skip to content
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

Support Rails 8 authentication generator #519

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jeromedalbert
Copy link

@jeromedalbert jeromedalbert commented Nov 11, 2024

Follow-up to rspec/rspec-rails#2811.

Problem

When running bin/rails generate authentication for a Rails 8 app that has the rspec-rails gem, generation ends with the following error: error factory_bot [not found].

This is because rspec-rails' AuthenticationGenerator contains hook_for :fixture_replacement, which calls factory_bot_rails, but it errors out since factory_bot_rails does not have an authentication generator.

Solution

Add authentication generator that creates a user factory with the email_address and password_digest fields. These fields come from the Rails authentication generator.

@jeromedalbert
Copy link
Author

rspec/rspec-rails#2811 was merged so this PR is ready for review.

@jeromedalbert jeromedalbert force-pushed the authentication-generator branch from 75cf803 to aa219dc Compare November 18, 2024 18:59
@jeromedalbert jeromedalbert changed the title Add Rails 8 authentication generator Support Rails 8 authentication generator Nov 20, 2024
@brentdodell
Copy link

Thanks for your work on this (and rspec-rails) @jeromedalbert! I ran into these issues while spinning up a new Rails 8 app.

Do we know what’s preventing it from being merged? I see one approval, but @neilvcarvalho, I see you added yourself as a reviewer recently.

Thanks!

@neilvcarvalho
Copy link
Member

Hi @brentdodell and @jeromedalbert! I'm going to review this PR (and other Rails 8 related work) this Friday, and possibly create a new release already.

brentdodell added a commit to brentdodell/rspec-test-generation-failure that referenced this pull request Feb 5, 2025
module Generators
class AuthenticationGenerator < Base
def create_fixture_file
template 'users.rb', 'test/factories/users.rb'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think test/factories/users.rb should be static here. It doesn't allow overriding things like the factory_bot directory, suffix, etc.

I think you'll need to include options, and build up the name dynamically.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing that the model generator does is checking if you already have a factory file, such as a global factories.rb instead of a factories directory. If you do, it appends the factory block to this file, instead of creating a new file.

Copy link
Member

@neilvcarvalho neilvcarvalho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides the comments made by @brentdodell and me, could you also add tests? We currently have a Cucumber feature test for that on features/generators.feature

module Generators
class AuthenticationGenerator < Base
def create_fixture_file
template 'users.rb', 'test/factories/users.rb'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another thing that the model generator does is checking if you already have a factory file, such as a global factories.rb instead of a factories directory. If you do, it appends the factory block to this file, instead of creating a new file.

@jeromedalbert
Copy link
Author

jeromedalbert commented Feb 8, 2025

could you also add tests? We currently have a Cucumber feature test for that on features/generators.feature

@neilvcarvalho rspec-rails does not currently have a released version containing the Rails 8 authentication generator; it's been on their main branch for now, as I suspect @JonRowe will prepare a separate release for Rails 8 support at some point. In the meantime, in order to add cucumber tests for the Rails 8 rspec-rails generator, I required rspec-rails as a git source in my test.

However the new test fails on my local. The error I get seems to be The git source https://github.com/rspec/rspec-rails.git is not yet checked out. Please run bundle install before trying to start your application (Bundler::GitError). I am not familiar with Cucumber so I may have missed something. I have pushed my work in progress test in this PR, so if someone else wants to have a look, throw ideas, or add to this PR, it's all good.

EDIT: It looks like feature specs are passing in CI, so I guess it's only an issue on my local.

@jeromedalbert jeromedalbert force-pushed the authentication-generator branch from e526fcc to ada4732 Compare February 9, 2025 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants