规范的相关片段看起来像这样:
let(:one_place) { create(:place) }
let(:other_place) { create(:place) }
let(:data) { "saved data" }
shared_examples "saves data to right place" do |right_place|
it { expect(right_place.data).to eq data }
end
context "when something it saves to one place" do
it_behaves_like "saves data to right place", one_place
end
context "when whatever it saves to other place" do
it_behaves_like "saves data to right place", other_place
end
Run Code Online (Sandbox Code Playgroud)
并且它可以与常量参数完美配合,但在这种情况下我收到一个错误:
one_place在示例组(例如adescribe或context块)上不可用.它只能从内individualexamples(例如it块),或从在一个例子(例如范围运行构建体before,let等).
在这种情况下如何将一个懒惰创建的变量传递给共享示例?