2017-10-27 3 views
1

this piece of documentationによると、形式documentationを選択することによって、次の出力形式を達成することが可能である:RSpecのドキュメントの出力形式

something 
    does something (SUCCESS) 
    does something (FAIL) 
    does something (PENDING) 

something 
    does something that passes 
    does something that fails (FAILED - 1) 
    does something that is pending (PENDING: Not Yet Implemented) 

は、それはそれとして出力して、わずかにこれを編集することが可能です

基本的には、明示的に失敗と保留中のものを記録するのではなく、テストの結果が何であっても表示されるようにしたいと思います。

答えて

1

RSpec::Core::Formatters::DocumentationFormatterをサブクラス化することでこれを実行できました。

rspec --require formatters/custom_formatter --format CustomFormatter 

代わりの--require formatters/custom_formatter、あなたもspec/spec_helper.rb上のフォーマッタを必要とすることができ、例えばこれを使用してスペックを実行し

class CustomFormatter < RSpec::Core::Formatters::DocumentationFormatter 
    RSpec::Core::Formatters.register self 

    private 

    def passed_output(example) 
    format_output(example, 'SUCCESS', :success) 
    end 

    def pending_output(example, _message) 
    format_output(example, 'PENDING', :pending) 
    end 

    def failure_output(example) 
    format_output(example, 'FAILED', :failure) 
    end 

    def format_output(example, status_text, code_or_symbol) 
    RSpec::Core::Formatters::ConsoleCodes.wrap(
     "#{current_indentation}#{example.description.strip} (#{status_text})", 
     code_or_symbol 
    ) 
    end 
end 

spec/formatters/custom_formatter.rbとして次のファイルを作成します。

require_relative 'formatters/custom_formatter' 

その後、あなたはこれだけ実行する必要があります:あなたはCustomFormatterは、デフォルトのフォーマッターになりたい場合は、あなたのプロジェクトのルートに.rspec configuration fileにコマンドラインオプションを追加することができます

rspec --format CustomFormatter 

を。ここではそれはのようになります方法は次のとおりです。CustomFormatterを使用することで

--require spec_helper 

--require /path/to/custom_formatter.rb 
--format CustomFormatter 

、あなたは、もはや任意のコマンドライン引数を指定する必要がありません。

マニュアル作成や参照:

+0

'SUCCESS'(PENDING''わからないために見事に動作します、しかし、それはいつでも私はそのエラーを与えます'fail_output:引数の数が2つ(1の2)(ArgumentError)'。 私は共同uldは、あなたが提供したメソッドのシグネチャを次のように編集して修正します。 'def failure_output(example、_message)' – Pauline

+0

@Paulineエラーがあっても動作するように管理してくれてうれしいです。 RSpecの古いバージョンを使用しているようです。 RSpec 3.5から、そのメソッドはただ1つの引数に変更されます。変更されたコミットは次のとおりです。https://github.com/rspec/rspec-core/commit/227e7cae419eee104764a8c9cfbf20b911cb63aa – htanata

0

既存のRSpecのフォーマッタを変更することはできませんが、RSpecの内蔵の出力フォーマッタは、しかし、あなたが必要 すべてを与えていないときは、独自のカスタムフォーマッタをcreate your own

書くことができますすることができますその代わりに を使用するようにRSpecに指示してください。最も簡単な方法は、RSpecのBaseTextFormatterをサブクラス化することです。 は、変更したいメソッドだけをオーバーライドします。