LoginSignup
2
1

More than 5 years have passed since last update.

rspec中にsystemの標準エラー出力を出力したくない

Last updated at Posted at 2018-07-03

バッチ処理などでsystemを使って処理をしているのですが、その際に処理中のメッセージなどが標準エラー出力に出力されます。
バッチ処理中にはそういったログが残って欲しいのですがrspec中に出力されるとテスト結果が長くなり邪魔です。

というわけで、rspecでsystemを実行する際にオプションにmergeして対応しました。

  before(:each) do 
    original_system = Object.method(:system)
    allow_any_instance_of(Object).to receive(:system) { | *args |
      option = {:out => "/dev/null", :err=>"/dev/null"}
      if args.last.is_a? Hash
        option = args.pop.merge(option)
      end 
      original_system.call(args.last, option)
    }
  end
2
1
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
2
1