Search code examples
rubythor

Testing a Thor CLI


I am writing a CLI using Thor. It looks like this:

module Cli
  class Root < Thor
    register Cli::Command :command, 'command [ARGUMENT]', 'description'
  end
end

module Cli
  class Command < Thor::Group
    include Thor::Actions

    argument :argument

    def template
      directory :template_dir, argument
    end
  end
end

Now I would like to test this. Namely, I want to test that the templates are created at the right directories given a relative and absolute path. I'm somewhat at a loss of how to go about testing this, however. Any help would be greatly appreciated.


Solution

  • In order to test a Thor::Group, you can do the following:

    # my_spec.rb
    describe Cli::Command do
      it 'should run the command' do
        Cli::Command.new.invoke_all
      end
    end