diff --git a/features/generators.feature b/features/generators.feature index d0e22c91..ecf9c47c 100644 --- a/features/generators.feature +++ b/features/generators.feature @@ -90,3 +90,21 @@ Feature: And I run `bundle exec rails generate model User name:string age:integer` with a clean environment Then the output should not contain "test/factories/users.rb" And the output should contain "test/fixtures/users.yml" + + + @rails_8 + Scenario: The factory_bot_rails authentication generator, coupled with rspec-rails, creates a user factory file + When I add "rspec-rails" as a dependency + And I run `bundle install --verbose` with a clean environment + Then the output should contain "rspec-rails" + And I run `bundle exec rails generate authentication` with a clean environment + Then the output should contain "spec/factories/users.rb" + And the file "spec/factories/users.rb" should contain exactly: + """ + FactoryBot.define do + factory :user do + email_address { "user@example.com" } + password { "password" } + end + end + """ diff --git a/features/support/env.rb b/features/support/env.rb index 02249f92..7a1b2bcb 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -11,6 +11,17 @@ setup_aruba end +Before("@rails_8") do + rails_version = Gem::Version.new( + Bundler.load.specs.find { |spec| spec.name == 'rails' }&.version.to_s + ) + required_version = Gem::Version.new('8.0.0') + + if rails_version < required_version + skip_this_scenario("Requires Rails 8.0 or higher (current: #{rails_version})") + end +end + if RUBY_PLATFORM == "java" Aruba.configure do |config| config.before_cmd do diff --git a/lib/generators/factory_bot/authentication/authentication_generator.rb b/lib/generators/factory_bot/authentication/authentication_generator.rb new file mode 100644 index 00000000..12eedbae --- /dev/null +++ b/lib/generators/factory_bot/authentication/authentication_generator.rb @@ -0,0 +1,23 @@ +require "generators/factory_bot" +require "generators/factory_bot/model/model_generator" +require "factory_bot_rails" + + +module FactoryBot + module Generators + FixedAttribute = Struct.new(:name, :default) + + class AuthenticationGenerator < ModelGenerator + self.source_paths << File.join(File.dirname(__FILE__), "../model/templates") + + private + + def attributes + [ + FixedAttribute.new(:email_address, "user@example.com"), + FixedAttribute.new(:password, "password") + ] + end + end + end +end diff --git a/lib/generators/factory_bot/authentication/templates/users.rb b/lib/generators/factory_bot/authentication/templates/users.rb new file mode 100644 index 00000000..ab38e1b1 --- /dev/null +++ b/lib/generators/factory_bot/authentication/templates/users.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :user do + password { "password123" } + password_confirmation { "password123" } + email_address { "user#{SecureRandom.hex(3)}@example.com" } + end +end