2017-07-05 11 views
0

RSpecでは、現在の例の現在のメタ記述を表す例文から文字列を生成するにはどうすればよいですか?例の完全な説明を表示

例:

RSpec.describe Api::V1::Public::Signin::ByEmailController, type: :controller do 
    describe 'POST #create' do 
    context 'when the provider is "email"' do 
     context 'when there is a saved email' do 
     context 'when the password is good' do 
      it 'signs in' do 
      expect(current_metadata_description).to eq 'Api::V1::Public::Signin::ByEmailController POST #create when the provider is "email" when there is a saved email when the password is good signs in' 
      end 
     end 
     end 
    end 
    end 
end 

我々が見ることができるように、与えられた例の現在のメタ記述がある:

アピ:: V1 ::公開::サインインも:: ByEmailController POST #createプロバイダが「電子メール」のときに保存された電子メールがあるときにパスワードが良好であるときに

ありがとう。

編集:

current_metadata_descriptionはRSpecのから提供されていません。私はちょうど私が持っているものの例を与えるためにそれを発明しました。

答えて

1

私はあなたの全体的な意図がこれを行うためのものであるかどうかわかりませんが。

Rspec::Core::ExampleGroup.itはブロックにRSpec::Core::Exampleをもたらす。すべての降伏方法と同様に、ブロックローカル変数に得られたObjectを取り込むことができます。 RSpec::Core::Exampleにはfull_descriptionメソッドがあります。これは探しているようです。 RSpec Source。例については

(しゃれが意図していない):

describe String do 
    context 'Down' do 
    context 'Deeper' do 
     it 'can describe itself' do |ex| 
     # ex is the specific RSpec::Core::Example for this it block 
     expect(ex.full_description).to eq('String Down Deeper can describe itself') 
     end 
    end 
    end 
end 

が、これは

. 
Finished in 0.003 seconds (files took 0.21998 seconds to load) 
1 example, 0 failures 

を返します実行しているあなたが所望の出力をしたい場合はブロック自体があるとして、あなたが得られた値をキャプチャすることが不可欠ですRSpec::Core::ExampleGroupのインスタンスのコンテキストで実行されるので、self.class.metadata[:full_description]を呼び出すとRSpec::Core::ExampleGroupの説明は含まれますが、RSpec::Core::Example(たとえば「文字列ダウンディーパー」)

これは、ログ記録の目的(または、各例については、この情報を希望似た何かをされた場合のドキュメントで説明したように)あなたがRSpec::Core::ExampleHooksで抜粋アクセスすることができます。

# Useful for configuring logging and/or taking some action based 
# on the state of an example's metadata. 
# 
#  RSpec.configure do |config| 
#  config.before do |example| 
#   log example.description 
#  end 
# 
#  config.after do |example| 
#   log example.description 
#  end 
# 
#  config.around do |example| 
#   log example.description 
#   example.run 
#  end 
#  end 
を本当にこの中にパッチを適用したい(と私はそれを支持しておりません)場合

は、あなたが述べたように、次の

class RSpec::Core::ExampleGroup 
    def instance_exec(*args,&blk) 
    if args.first.is_a?(RSpec::Core::Example) 
     self.define_singleton_method(:current_metadata_description) do 
     args.first.full_description 
     end 
    end 
    super(*args,&blk) 
    end 
end 

は、その後、あなたのテストが正確だろう行うことができます。

0

--format optionとRSpecの実行してみます:

bundle exec rspec --format documentation 

更新

私はあなたのためにそれを行うだろう任意のRSpecの方法を知りません。私が考えることのできる1つの方法は、テストを実行している間にファイルにスペックの記述を書き込んだり、独自のcurrent_metadata_descriptionメソッドでそれから記述を読み取ったりすることです。ここにはGenerating documentation from specs.に関するチュートリアルがあります。

+0

返信いただきありがとうございます。しかし、私は 'current_metadata_description'のようなメソッドを持っていて、現在のメタデータ記述を例の中から取り出すことを望みます。 –

+0

このリンクは、あなたが求めていることをする上でアイデアを提供するように見えます。 –

関連する問題