@@ -8,21 +8,21 @@ class AuthenticationGeneratorTest < Rails::Generators::TestCase
8
8
include GeneratorsTestHelper
9
9
10
10
def setup
11
- Rails . application = TestApp ::Application
12
- Rails . application . config . root = Pathname ( destination_root )
11
+ FileUtils . mkdir_p ( "#{ destination_root } /app/controllers" )
12
+ File . write ( "#{ destination_root } /app/controllers/application_controller.rb" , <<~RUBY )
13
+ class ApplicationController < ActionController::Base
14
+ end
15
+ RUBY
13
16
14
- self . class . tests Rails ::Generators ::AppGenerator
15
- run_generator ( [ destination_root , "--no-skip-bundle" ] )
17
+ copy_gemfile
16
18
17
- self . class . tests Rails ::Generators ::AuthenticationGenerator
18
- end
19
-
20
- def teardown
21
- Rails . application = Rails . application . instance
19
+ copy_routes
22
20
end
23
21
24
22
def test_authentication_generator
25
- run_generator
23
+ generator ( [ destination_root ] )
24
+
25
+ run_generator_instance
26
26
27
27
assert_file "app/models/user.rb"
28
28
assert_file "app/models/current.rb"
@@ -46,30 +46,27 @@ def test_authentication_generator
46
46
assert_match ( /resource :session/ , content )
47
47
end
48
48
49
- assert_migration "db/migrate/create_sessions.rb" do |content |
50
- assert_match ( /t.references :user, null: false, foreign_key: true/ , content )
51
- end
52
-
53
- assert_migration "db/migrate/create_users.rb" do |content |
54
- assert_match ( /t.string :password_digest, null: false/ , content )
55
- end
49
+ assert_includes @rails_commands , "generate migration CreateUsers email_address:string!:uniq password_digest:string! --force"
50
+ assert_includes @rails_commands , "generate migration CreateSessions user:references ip_address:string user_agent:string --force"
56
51
57
52
assert_file "test/models/user_test.rb"
58
53
assert_file "test/fixtures/users.yml"
59
54
end
60
55
61
56
def test_authentication_generator_without_bcrypt_in_gemfile
62
- File . write ( "Gemfile" , File . read ( "Gemfile" ) . sub ( /# gem "bcrypt".*\n / , "" ) )
57
+ File . write ( "#{ destination_root } / Gemfile" , File . read ( "#{ destination_root } / Gemfile" ) . sub ( /# gem "bcrypt".*\n / , "" ) )
63
58
64
- run_generator
59
+ generator ( [ destination_root ] )
65
60
66
- assert_file "Gemfile" do | content |
67
- assert_match ( / \n gem "bcrypt"/ , content )
68
- end
61
+ run_generator_instance
62
+
63
+ assert_includes @bundle_commands , [ :bundle , "add bcrypt" , { capture : true } ]
69
64
end
70
65
71
66
def test_authentication_generator_with_api_flag
72
- run_generator ( [ "--api" ] )
67
+ generator ( [ destination_root ] , api : true )
68
+
69
+ run_generator_instance
73
70
74
71
assert_file "app/models/user.rb"
75
72
assert_file "app/models/current.rb"
@@ -93,21 +90,40 @@ def test_authentication_generator_with_api_flag
93
90
assert_match ( /resource :session/ , content )
94
91
end
95
92
96
- assert_migration "db/migrate/create_sessions.rb" do |content |
97
- assert_match ( /t.references :user, null: false, foreign_key: true/ , content )
98
- end
99
-
100
- assert_migration "db/migrate/create_users.rb" do |content |
101
- assert_match ( /t.string :password_digest, null: false/ , content )
102
- end
93
+ assert_includes @rails_commands , "generate migration CreateUsers email_address:string!:uniq password_digest:string! --force"
94
+ assert_includes @rails_commands , "generate migration CreateSessions user:references ip_address:string user_agent:string --force"
103
95
104
96
assert_file "test/models/user_test.rb"
105
97
assert_file "test/fixtures/users.yml"
106
98
end
107
99
108
100
def test_model_test_is_skipped_if_test_framework_is_given
109
- content = run_generator [ "authentication" , "-t" , "rspec" ]
101
+ generator ( [ destination_root ] , [ "-t" , "rspec" ] )
102
+
103
+ content = run_generator_instance
104
+
110
105
assert_match ( /rspec \[ not found\] / , content )
111
106
assert_no_file "test/models/user_test.rb"
112
107
end
108
+
109
+ private
110
+
111
+ def run_generator_instance
112
+ commands = [ ]
113
+ command_stub ||= -> ( command , *args ) { commands << [ command , *args ] }
114
+
115
+ @rails_commands = [ ]
116
+ @rails_command_stub ||= -> ( command , *_ ) { @rails_commands << command }
117
+
118
+ content = nil
119
+ generator . stub ( :execute_command , command_stub ) do
120
+ generator . stub ( :rails_command , @rails_command_stub ) do
121
+ content = super
122
+ end
123
+ end
124
+
125
+ @bundle_commands = commands . filter { |command , _ | command == :bundle }
126
+
127
+ content
128
+ end
113
129
end
0 commit comments