module Rails::Generators::Testing::Behaviour
def create_generated_attribute(attribute_type, name = 'test', index = nil)
attribute type and, optionally, the attribute name:
Create a Rails::Generators::GeneratedAttribute by supplying the
def create_generated_attribute(attribute_type, name = 'test', index = nil) Rails::Generators::GeneratedAttribute.parse([name, attribute_type, index].compact.join(':')) end
def destination_root_is_set? # :nodoc:
def destination_root_is_set? # :nodoc: raise "You need to configure your Rails::Generators::TestCase destination root." unless destination_root end
def ensure_current_path # :nodoc:
def ensure_current_path # :nodoc: cd current_path end
def generator(args=self.default_arguments, options={}, config={})
def generator(args=self.default_arguments, options={}, config={}) @generator ||= self.generator_class.new(args, options, config.reverse_merge(destination_root: destination_root)) end
def migration_file_name(relative) # :nodoc:
def migration_file_name(relative) # :nodoc: absolute = File.expand_path(relative, destination_root) dirname, file_name = File.dirname(absolute), File.basename(absolute).sub(/\.rb$/, '') Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first end
def prepare_destination
def prepare_destination rm_rf(destination_root) mkdir_p(destination_root) end
def run_generator(args=self.default_arguments, config={})
You can provide a configuration hash as second argument. This method returns the output
end
end
assert_no_file "config/database.yml"
run_generator %w(myapp --skip-active-record)
test "database.yml is not created when skipping Active Record" do
setup :prepare_destination
destination File.expand_path("../tmp", File.dirname(__FILE__))
tests AppGenerator
class AppGeneratorTest < Rails::Generators::TestCase
command line arguments:
Runs the generator configured for this class. The first argument is an array like
def run_generator(args=self.default_arguments, config={}) capture(:stdout) do args += ['--skip-bundle'] unless args.include? '--dev' self.generator_class.start(args, config.reverse_merge(destination_root: destination_root)) end end