Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RSpec:如何通过a";让我们;变量作为共享示例的参数_Ruby On Rails_Rspec - Fatal编程技术网

Ruby on rails RSpec:如何通过a";让我们;变量作为共享示例的参数

Ruby on rails RSpec:如何通过a";让我们;变量作为共享示例的参数,ruby-on-rails,rspec,Ruby On Rails,Rspec,规范的相关片段如下所示: 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 i

规范的相关片段如下所示:

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
它可以完美地处理常量参数,但在这种情况下,我收到一个错误:

示例组(例如
描述
上下文
块)上没有
一个位置
。它只能从单个示例(例如,
It
块)或在示例范围内运行的构造(例如,
before
let
等)中获得

在这种情况下,如何将延迟创建的变量传递给共享示例

在这种情况下,如何将延迟创建的变量传递给共享示例

好的旧回忆录怎么了

def one_place
  @one_place ||= create(:place)
end
从中,我认为您需要将
let
语句放入传递给
的块中,它的行为类似于

let(:data) { "saved data" }

shared_examples "saves data to right place" do
  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" do
    let(:right_place) { create(:place) }
  end
end

context "when whatever it saves to other place" do  
  it_behaves_like "saves data to right place" do
    let(:right_place) { create(:place) }
  end
end
  • 您也可以按如下方式使用let变量:
  • 共享_示例“将数据保存到正确的位置”执行 它{expect(right_place.data).to eq data} 结束 上下文“当某物保存到一个地方时”执行 let(:right_place){create(:place)} 其行为类似于“将数据保存到正确的位置” 结束 上下文“当它保存到其他地方时”执行 let(:right_place){create(:place)} 其行为类似于“将数据保存到正确的位置” 结束 注:仅在插值的情况下使用双引号

  • 您可以按如下方式使规格干燥:
  • %w(一个另一个)。每个do |位置| let(:right_place){location==one?create(:place):create(:place,:other)} 上下文“当它保存到#{location}place”时 其行为类似于“将数据保存到正确的位置” 结束 结束
    我要指出的是,不幸的是,你试图实现的目标是不可能的。这将是可取的,因为它使这些变量的使用变得明确。上面提到的解决方法(define-let-where-you-use
    it\u-behavior\u-like
    )很有效,但我发现这样编写的共享示例令人困惑

    我使用一个共享示例在共享示例中明确了所需变量:

    RSpec.shared_examples "requires variables" do |variable_names|
      it "(shared example requires `#{variable_names}` to be set)" do
        variable_names = variable_names.kind_of?(Array) ? variable_names : [variable_names]
        temp_config = RSpec::Expectations.configuration.on_potential_false_positives
        RSpec::Expectations.configuration.on_potential_false_positives = :nothing
    
        variable_names.each do |variable_name|
          expect { send(variable_name) }.not_to(
            raise_error(NameError), "The following variables must be set to use this shared example: #{variable_names}"
          )
        end
    
        RSpec::Expectations.configuration.on_potential_false_positives = temp_config
      end
    end
    
    像这样使用它:

    RSpec.shared_examples "saves data to right place" do
      include_examples "requires variables", :right_place
    
      # ...
    end
    
    context "..." do
      it_behaves_like "saves data to right place" do
        let(:right_place) { "" }
      end
    end
    

    奇怪的是,这样的构造在
    it{}
    块之外引发了同样的错误,就是同一个错误。我也不明白为什么,但我仔细检查了一下,没有发现任何错误。
    RSpec.shared_examples "requires variables" do |variable_names|
      it "(shared example requires `#{variable_names}` to be set)" do
        variable_names = variable_names.kind_of?(Array) ? variable_names : [variable_names]
        temp_config = RSpec::Expectations.configuration.on_potential_false_positives
        RSpec::Expectations.configuration.on_potential_false_positives = :nothing
    
        variable_names.each do |variable_name|
          expect { send(variable_name) }.not_to(
            raise_error(NameError), "The following variables must be set to use this shared example: #{variable_names}"
          )
        end
    
        RSpec::Expectations.configuration.on_potential_false_positives = temp_config
      end
    end
    
    RSpec.shared_examples "saves data to right place" do
      include_examples "requires variables", :right_place
    
      # ...
    end
    
    context "..." do
      it_behaves_like "saves data to right place" do
        let(:right_place) { "" }
      end
    end