module Quails::Generators::Testing::Behaviour

Public Instance Methods

create_generated_attribute(attribute_type, name = "test", index = nil) click to toggle source

Create a Quails::Generators::GeneratedAttribute by supplying the attribute type and, optionally, the attribute name:

create_generated_attribute(:string, 'name')
# File railties/lib/rails/generators/testing/behaviour.rb, line 83
def create_generated_attribute(attribute_type, name = "test", index = nil)
  Quails::Generators::GeneratedAttribute.parse([name, attribute_type, index].compact.join(":"))
end
generator(args = default_arguments, options = {}, config = {}) click to toggle source

Instantiate the generator.

# File railties/lib/rails/generators/testing/behaviour.rb, line 75
def generator(args = default_arguments, options = {}, config = {})
  @generator ||= generator_class.new(args, options, config.reverse_merge(destination_root: destination_root))
end
run_generator(args = default_arguments, config = {}) click to toggle source

Runs the generator configured for this class. The first argument is an array like command line arguments:

class AppGeneratorTest < Quails::Generators::TestCase
  tests AppGenerator
  destination File.expand_path("../tmp", __dir__)
  setup :prepare_destination

  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.

# File railties/lib/rails/generators/testing/behaviour.rb, line 67
def run_generator(args = default_arguments, config = {})
  capture(:stdout) do
    args += ["--skip-bundle"] unless args.include? "--dev"
    generator_class.start(args, config.reverse_merge(destination_root: destination_root))
  end
end

Private Instance Methods

destination_root_is_set?() click to toggle source
# File railties/lib/rails/generators/testing/behaviour.rb, line 89
def destination_root_is_set?
  raise "You need to configure your Quails::Generators::TestCase destination root." unless destination_root
end
ensure_current_path() click to toggle source
# File railties/lib/rails/generators/testing/behaviour.rb, line 93
def ensure_current_path
  cd current_path
end
migration_file_name(relative) click to toggle source
# File railties/lib/rails/generators/testing/behaviour.rb, line 103
def migration_file_name(relative)
  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
prepare_destination() click to toggle source

Clears all files and directories in destination.

# File railties/lib/rails/generators/testing/behaviour.rb, line 98
def prepare_destination # :doc:
  rm_rf(destination_root)
  mkdir_p(destination_root)
end