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 已经创建了一个新的上下文,所以我认为最干净的方法就像 shioyama 的例子,没有额外的上下文:

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

Let 在每个块之前进行评估,it但不是针对contextdescribe据我所知。

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 回答