【问题标题】:Let variable undefined in shared example rspec让共享示例 rspec 中的变量未定义
【发布时间】:2018-06-20 01:56:00
【问题描述】:

我有这个代码

describe ChocolateTotalsCalculator do

  shared_examples "a fully redeemed set of chocolates" do
    it "returns a final set of chocolates: #{final_chocolates}" do
      expect(subject).to eq(final_chocolates)
    end
  end

  describe "#calculate" do
    subject { chocolate_calculator.calculate }

    context "wrappers are redeemed zero times" do
      context "row is [cash: 0, price: 0, wrappers: 0, type: 'dark']" do

        it_behaves_like "a fully redeemed set of chocolates" do
          let(:row) { CSV::Row.new(csv_headers,[0,0,0,"dark"]) }
          let(:final_chocolates) {
            {
              "milk" => 0,
              "dark" => 0,
              "white" => 0,
              "sugar free" => 0
            }
          }
        end
      end

但我收到此错误:

NameError:
  undefined local variable or method `final_chocolates' for #<Class:0x00007f9322b6c148>

发生了什么?为什么我不能以这种方式使用共享示例?

【问题讨论】:

    标签: rspec


    【解决方案1】:

    问题出在这一行

    it "returns a final set of chocolates: #{final_chocolates}" do
    

    final_chocolates 仅在示例中可用(例如 it 块),在示例组中不可用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多