在共享示例rspec中让变量未定义

时间:2018-06-20 01:56:01

标签: rspec

我有这个代码

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>

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

1 个答案:

答案 0 :(得分:0)

问题出在这条线上

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

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