Skip to content

Commit dfcb27e

Browse files
committed
Fix authentication_generator_test
Those tests started to fail as soon as we started developing 8.1 since it is not present on rubygems. We should not have to generate a new app and install gems to be able to test the behavior of this generator. I used the same techniques we use in the application generator mocking the commands that are executed and generating only the files that are necessary to test the behavior of this generator.
1 parent 5a51fd9 commit dfcb27e

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def enable_bcrypt
4646
end
4747

4848
def add_migrations
49-
generate "migration CreateUsers email_address:string!:uniq password_digest:string! --force"
50-
generate "migration CreateSessions user:references ip_address:string user_agent:string --force"
49+
generate "migration", "CreateUsers", "email_address:string!:uniq password_digest:string!", "--force"
50+
generate "migration", "CreateSessions", "user:references ip_address:string user_agent:string", "--force"
5151
end
5252

5353
hook_for :test_framework

railties/test/generators/authentication_generator_test.rb

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ class AuthenticationGeneratorTest < Rails::Generators::TestCase
88
include GeneratorsTestHelper
99

1010
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
1316

14-
self.class.tests Rails::Generators::AppGenerator
15-
run_generator([destination_root, "--no-skip-bundle"])
17+
copy_gemfile
1618

17-
self.class.tests Rails::Generators::AuthenticationGenerator
18-
end
19-
20-
def teardown
21-
Rails.application = Rails.application.instance
19+
copy_routes
2220
end
2321

2422
def test_authentication_generator
25-
run_generator
23+
generator([destination_root])
24+
25+
run_generator_instance
2626

2727
assert_file "app/models/user.rb"
2828
assert_file "app/models/current.rb"
@@ -46,30 +46,27 @@ def test_authentication_generator
4646
assert_match(/resource :session/, content)
4747
end
4848

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"
5651

5752
assert_file "test/models/user_test.rb"
5853
assert_file "test/fixtures/users.yml"
5954
end
6055

6156
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/, ""))
6358

64-
run_generator
59+
generator([destination_root])
6560

66-
assert_file "Gemfile" do |content|
67-
assert_match(/\ngem "bcrypt"/, content)
68-
end
61+
run_generator_instance
62+
63+
assert_includes @bundle_commands, [:bundle, "add bcrypt", { capture: true }]
6964
end
7065

7166
def test_authentication_generator_with_api_flag
72-
run_generator(["--api"])
67+
generator([destination_root], api: true)
68+
69+
run_generator_instance
7370

7471
assert_file "app/models/user.rb"
7572
assert_file "app/models/current.rb"
@@ -93,21 +90,40 @@ def test_authentication_generator_with_api_flag
9390
assert_match(/resource :session/, content)
9491
end
9592

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"
10395

10496
assert_file "test/models/user_test.rb"
10597
assert_file "test/fixtures/users.yml"
10698
end
10799

108100
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+
110105
assert_match(/rspec \[not found\]/, content)
111106
assert_no_file "test/models/user_test.rb"
112107
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
113129
end

0 commit comments

Comments
 (0)