Skip to content

Instantly share code, notes, and snippets.

@stevepolitodesign
Created February 9, 2022 10:49
Show Gist options
  • Save stevepolitodesign/05258a70476c9958fa75fa1f0902005f to your computer and use it in GitHub Desktop.
Save stevepolitodesign/05258a70476c9958fa75fa1f0902005f to your computer and use it in GitHub Desktop.
Testing a Rails generator

Generator

class DemoGenerator < Rails::Generators::Base
  source_root File.expand_path("templates", __dir__)

  def make_table
    generate "migration", "create_demos_table name:string"
  end
end

Test

require "test_helper"
require "generators/demo/demo_generator"

module MyEngine
  class DemoGeneratorTest < Rails::Generators::TestCase
    tests DemoGenerator
    destination Rails.root.join("tmp/generators")
    setup :prepare_destination

    test "generator runs without errors" do
      assert_nothing_raised do
        run_generator
      end
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment