LoginSignup
53
57

More than 5 years have passed since last update.

Thorで作ったコマンドラインインターフェースをRspecでテスト

Last updated at Posted at 2013-12-17

Thorで作ったコマンドラインインターフェース(CLI)のクラスをRSpecでテストする方法

  • ThorでCLIを作成すると以下のようにThorクラス継承したクラスを作る事になる。
    • 例えば以下のようなクラスを作ったとする
class MyCLI < Thor

  desc "run", "run task"
  method_option :output
  def run
     options[:output]
  end
end
  • このテストをする際に以下のように書くとoptionsに値をセットする事ができない。
it "should display output" do
  Sample.new.run.should eq "xxx"
end
  • optionsの値もセットした状態で実行するにはinvokeメソッドを使用する。
    • 第一引数はメソッド名、第二引数は引数、第三引数はoptionsの値をhashで渡す。
it "should display output" do
  Sample.new.invoke(:run, [], {output: "xxx"}).should eq "xxx"
end
53
57
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
53
57