0

タイトルの言い回しがよくわかりませんでしたが、私の問題は次のとおりです。

shared_examples "something" do 
  context "for something" do 
    fields.each do |field| 
      it "should have #{field} field" do 
        #Check something 
      end
    end
  end
end

describe Clazz do
  it_behaves_like "something" do
    let(:fields) {%w{something something2}}
  end
end

fields.eachもちろん、変数はitスコープではなくスコープに導入されるため、実行は部分的に爆発しcontextます。

だから私の質問は、コンテキストスコープにit_behaves_likeで変数をどのように導入するのですか?または、他のものを使用する必要があります。

4

3 に答える 3

2

についてはわかりませんがshared_examples、使用するshared_examples_for場合は、次のようにブロックに引数を渡すことができます。

shared_examples_for "something" do |fields|
  context "for something" do
    fields.each do |field| 
      it "should have #{field} field" do 
        #Check something 
      end
    end
  end
end

describe Clazz do
  it_behaves_like "something", %w{something something2}
end
于 2012-09-23T13:05:41.150 に答える
2

shared_examplesはすでに新しいコンテキストを作成しているので、最もクリーンな方法は、追加のコンテキストがない塩山の例のようになると思います。

shared_examples_for "something" do |fields|
  fields.each do |field| 
    it "should have #{field} field" do 
      # specify something
    end
  end
end

describe Clazz do
  it_behaves_like "something", %w{something something2}
end
于 2012-09-23T13:42:55.553 に答える
0

Letは各itブロックの前に評価されますが、私が知っている限りでは評価されcontextません。describe

describe "something" do 
  let(:fields) { %w{something something2} }

  it "should have all fields" do 
    fields.each do |field| 
    end
  end
end
于 2012-09-23T11:23:49.960 に答える