2016-12-05 6 views
0

私はcapybaraのrspec統合テストの初心者です。 動的に計算されたパラメータを持つ共有サンプルを呼び出すにはどうすればよいですか?動的計算パラメータを使用した共有サンプルの呼び出し

shared_examples_for "a measurable object" do |example, display_name| 
    it "is example - #{display_name}" do 
     visit "www.example.com?args=test" 
     expect(page.find("#examplediv").text).to eq example 
    end 
end 

describe "example" do 
    # where to compute this dynamic_value 
    it_behaves_like "a measurable object", dynamic_value, "example 1" 
end 

describeとshared_exampleの両方が別々のファイルにあります。

上記のコードスニペットでは、メソッド呼び出しから取得したデータに基づいてdynamic_valueを計算します。

「dynamic_value」の値はどこで計算されますか?

私はbefore :eachbefore :allで計算しようとしましたが、どちらもうまくいきませんでした。

私は説明文を使ってコールサイクルを説明するといいかもしれません。

ありがとうございます。

+0

これは、「コールサイクル」http://www.wulftone.com/2012/01/22/rspec-gotchas-before-after-all-and-each/で少なくともお手伝いします。それ以外のあなたの質問は私には少し不明です – engineersmnky

+0

そのブログは共有の例について話していませんし、it_behaves_likeが引数に基づいて一意の "it"ブロックを作成するために前処理されています。 –

+0

あなたは記述ブロックの呼び出しサイクルを尋ね、 'before:each'と' before:all'を使用してコメントをしました。そうでなければ、質問に基づいて渡される唯一の値は、文字列「動的値」( 'expect(example).to eq「動的値」)です。 – engineersmnky

答えて

1

私はあなたの意図を理解しているとはまだ100%は確信していませんが、今私は基本的な説明を提供するのに十分であると思います。私のようなこの概念を実装します:

shared_examples_for "a page parser" do |dom_object,value| 
    it "the text in #{dom_object} should equal #{value} on #{url}" do 
    visit url 
    expect(page.find("##{dom_object}").text).to eq value 
    end 
end 

describe "example" do 
    let(:url) { "www.example.com?args=test" }  
    values_obtained_from_service_call = Service.call(url) 
    # We will assume this is something like [{dom_object: examplediv, value: "Hello World!"}] 
    values_obtained_from_service_call.each do |test| 
    it_should_behave_like "a page parser", test[:dom_object], test[:value] 
    end 
end 

これはvalues_obtained_from_service_callを反復処理し、共有の例を使用して、それらすべてをテストします。

私が言ったように、なぜあなたがこれをやりたいのかまだ分かりませんが、機能的にはうまくいくはずです。

関連する問題