0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rspecでプログラム内(実装コード内)の標準出力を無効にする

Posted at

概要

実装内で p とか puts とかして標準出力してる場合など、Rspecの実行結果にもそれが紛れてしまうことがあるが、その対策

もちろんRspec自体が吐き出す標準出力はそのままで、実装内のものだけを除外したい

設定例

rails_helper に以下を設定

RSpec.configure do |config|
  config.before(:all) do
    File.write(File.join(File.dirname(__FILE__), 'dev', 'null.txt'), 'w')

    @original_stderr = $stderr
    @original_stdout = $stdout
    $stderr = File.new(File.join(File.dirname(__FILE__), 'dev', 'null.txt'), 'w')
    $stdout = File.new(File.join(File.dirname(__FILE__), 'dev', 'null.txt'), 'w')
  end

  config.after(:all) do
    $stderr = @original_stderr
    $stdout = @original_stdout
    @original_stderr = nil
    @original_stdout = nil
  end

標準出力の内容は spec/dev/null.txt に書き出されるようになる
あとは spec/dev/null.txt を gitignore しておくと良い

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

プロフィール・経歴

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?