Skip to content

Commit ff42150

Browse files
authored
Add user fixture to authentication generator (rails#53165)
1 parent 646d5da commit ff42150

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

railties/lib/rails/generators/rails/authentication/authentication_generator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def add_migrations
4949
generate "migration CreateUsers email_address:string!:uniq password_digest:string! --force"
5050
generate "migration CreateSessions user:references ip_address:string user_agent:string --force"
5151
end
52+
53+
hook_for :test_framework
5254
end
5355
end
5456
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
require "rails/generators/test_unit"
4+
5+
module TestUnit # :nodoc:
6+
module Generators # :nodoc:
7+
class AuthenticationGenerator < Rails::Generators::Base # :nodoc:
8+
def create_user_test_files
9+
template "test/fixtures/users.yml"
10+
template "test/models/user_test.rb"
11+
end
12+
end
13+
end
14+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<%% password_digest = BCrypt::Password.create("password") %>
2+
3+
one:
4+
email_address: [email protected]
5+
password_digest: <%%= password_digest %>
6+
7+
two:
8+
email_address: [email protected]
9+
password_digest: <%%= password_digest %>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "test_helper"
2+
3+
class UserTest < ActiveSupport::TestCase
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

railties/test/generators/authentication_generator_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def test_authentication_generator
5353
assert_migration "db/migrate/create_users.rb" do |content|
5454
assert_match(/t.string :password_digest, null: false/, content)
5555
end
56+
57+
assert_file "test/models/user_test.rb"
58+
assert_file "test/fixtures/users.yml"
5659
end
5760

5861
def test_authentication_generator_without_bcrypt_in_gemfile
@@ -97,5 +100,14 @@ def test_authentication_generator_with_api_flag
97100
assert_migration "db/migrate/create_users.rb" do |content|
98101
assert_match(/t.string :password_digest, null: false/, content)
99102
end
103+
104+
assert_file "test/models/user_test.rb"
105+
assert_file "test/fixtures/users.yml"
106+
end
107+
108+
def test_model_test_is_skipped_if_test_framework_is_given
109+
content = run_generator ["authentication", "-t", "rspec"]
110+
assert_match(/rspec \[not found\]/, content)
111+
assert_no_file "test/models/user_test.rb"
100112
end
101113
end

0 commit comments

Comments
 (0)