Skip to content

Commit 8b7e682

Browse files
authored
Merge pull request rails#51176 from stevepolitodesign/assert-initializer
Introduce `Rails::Generators::Testing::Assertions#assert_initializer`
2 parents cb47c12 + fb16702 commit 8b7e682

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

railties/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
* Introduce `Rails::Generators::Testing::Assertions#assert_initializer`
2+
3+
Compliments the existing `initializer` generator action.
4+
5+
```rb
6+
assert_initializer "mail_interceptors.rb"
7+
```
8+
9+
*Steve Polito*
10+
111
* Generate a .devcontainer folder and its contents when creating a new app.
212

313
The .devcontainer folder includes everything needed to boot the app and do development in a remote container.

railties/lib/rails/generators/testing/assertions.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,26 @@ def assert_field_default_value(attribute_type, value)
121121
assert_equal(value, create_generated_attribute(attribute_type).default)
122122
end
123123
end
124+
125+
# Asserts a given initializer exists. You need to supply a path relative
126+
# to the `config/initializers/` directory.
127+
#
128+
# assert_initializer "mail_interceptors.rb"
129+
#
130+
# You can also give extra arguments. If the argument is a regexp, it will check if the
131+
# regular expression matches the given file content. If it's a string, it compares the
132+
# file with the given string:
133+
#
134+
# assert_initializer "mail_interceptors.rb", /SandboxEmailInterceptor/
135+
#
136+
# Finally, when a block is given, it yields the file content:
137+
#
138+
# assert_initializer "mail_interceptors.rb" do |initializer|
139+
# assert_match(/SandboxEmailInterceptor/, initializer)
140+
# end
141+
def assert_initializer(name, *contents)
142+
assert_file("config/initializers/#{name}", *contents)
143+
end
124144
end
125145
end
126146
end

railties/test/generators/actions_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ def test_rakefile_should_write_date_to_file_with_block_in_lib_tasks
401401

402402
def test_initializer_should_write_date_to_file_in_config_initializers
403403
action :initializer, "constants.rb", "MY_CONSTANT = 42"
404-
assert_file "config/initializers/constants.rb", "MY_CONSTANT = 42\n"
404+
assert_initializer "constants.rb", "MY_CONSTANT = 42\n"
405405
end
406406

407407
def test_initializer_should_write_date_to_file_with_block_in_config_initializers
@@ -411,7 +411,7 @@ def test_initializer_should_write_date_to_file_with_block_in_config_initializers
411411
end
412412
RUBY
413413
action(:initializer, "constants.rb") { code }
414-
assert_file "config/initializers/constants.rb", code.strip_heredoc
414+
assert_initializer "constants.rb", code.strip_heredoc
415415
end
416416

417417
test "generate" do

0 commit comments

Comments
 (0)