This class provides a TestCase for testing generators. To setup, you need just to configure the destination and set which generator is being tested:
class AppGeneratorTest < Rails::Generators::TestCase tests AppGenerator destination File.expand_path("../tmp", File.dirname(__FILE__)) end
If you want to ensure your destination root is clean before running each test, you can set a setup callback:
class AppGeneratorTest < Rails::Generators::TestCase tests AppGenerator destination File.expand_path("../tmp", File.dirname(__FILE__)) setup :prepare_destination end
- A
- C
- D
- G
- P
- R
- T
- FileUtils START:includes
Sets default arguments on generator invocation. This can be overwritten when invoking it.
arguments %w(app_name --skip-active-record)
Sets the destination of generator files:
destination File.expand_path("../tmp", File.dirname(__FILE__))
Sets which generator should be tested:
tests AppGenerator
Asserts the given class method exists in the given content. This method does not detect class methods inside (class << self), only class methods which starts with “self.”. When a block is given, it yields the content of the method.
assert_migration "db/migrate/create_products.rb" do |migration| assert_class_method :up, migration do |up| assert_match(/create_table/, up) end end
Alias for assert_file
Asserts the given attribute type gets a proper default value:
assert_field_default_value :string, "MyString"
Asserts the given attribute type gets translated to a field type properly:
assert_field_type :date, :date_select
Asserts a given file exists. You need to supply an absolute path or a path relative to the configured destination:
assert_file "config/environment.rb"
You can also give extra arguments. If the argument is a regexp, it will check if the regular expression matches the given file content. If it’s a string, it compares the file with the given string:
assert_file "config/environment.rb", /initialize/
Finally, when a block is given, it yields the file content:
assert_file "app/controller/products_controller.rb" do |controller| assert_instance_method :index, content do |index| assert_match(/Product\.all/, index) end end
# File railties/lib/rails/generators/test_case.rb, line 88 88: def assert_file(relative, *contents) 89: absolute = File.expand_path(relative, destination_root) 90: assert File.exists?(absolute), "Expected file #{relative.inspect} to exist, but does not" 91: 92: read = File.read(absolute) if block_given? || !contents.empty? 93: yield read if block_given? 94: 95: contents.each do |content| 96: case content 97: when String 98: assert_equal content, read 99: when Regexp 100: assert_match content, read 101: end 102: end 103: end
Asserts the given method exists in the given content. When a block is given, it yields the content of the method.
assert_file "app/controller/products_controller.rb" do |controller| assert_instance_method :index, content do |index| assert_match(/Product\.all/, index) end end
Alias for assert_instance_method
Asserts a given migration exists. You need to supply an absolute path or a path relative to the configured destination:
assert_migration "db/migrate/create_products.rb"
This method manipulates the given path and tries to find any migration which matches the migration name. For example, the call above is converted to:
assert_file "db/migrate/003_create_products.rb"
Consequently, assert_migration accepts the same arguments has assert_file.
# File railties/lib/rails/generators/test_case.rb, line 129 129: def assert_migration(relative, *contents, &block) 130: file_name = migration_file_name(relative) 131: assert file_name, "Expected migration #{relative} to exist, but was not found" 132: assert_file file_name, *contents, &block 133: end
Alias for assert_no_file
Asserts a given file does not exist. You need to supply an absolute path or a path relative to the configured destination:
assert_no_file "config/random.rb"
Asserts a given migration does not exist. You need to supply an absolute path or a path relative to the configured destination:
assert_no_migration "db/migrate/create_products.rb"
Create a Rails::Generators::GeneratedAttribute by supplying the attribute type and, optionally, the attribute name:
create_generated_attribute(:string, 'name')
Instantiate the generator.
Runs the generator configured for this class. The first argument is an array like command line arguments:
class AppGeneratorTest < Rails::Generators::TestCase tests AppGenerator destination File.expand_path("../tmp", File.dirname(__FILE__)) teardown :cleanup_destination_root test "database.yml is not created when skipping Active Record" do run_generator %w(myapp --skip-active-record) assert_no_file "config/database.yml" end end
You can provide a configuration hash as second argument. This method returns the output printed by the generator.