小编mpi*_*ask的帖子

RSpec:如何将"let"变量作为参数传递给共享示例

规范的相关片段看起来像这样:

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在示例组(例如a describecontext块)上不可用.它只能从内individualexamples(例如it块),或从在一个例子(例如范围运行构建体before,let等).

在这种情况下如何将一个懒惰创建的变量传递给共享示例?

rspec ruby-on-rails

6
推荐指数
2
解决办法
2574
查看次数

标签 统计

rspec ×1

ruby-on-rails ×1