rspec - 让共享示例 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>
怎么了?为什么我不能以这种方式使用共享示例?
解决方案
问题出在这条线上
it "returns a final set of chocolates: #{final_chocolates}" do
final_chocolates
仅在示例(例如it
块)中可用,在示例组中不可用。
推荐阅读
- python - 从python中的字符串中分离字母和数字部分
- c++ - 使用 V4l2 捕获的视频丢帧/丢帧
- video-streaming - 有什么方法可以估计使用 VNC 所需的带宽吗?
- uitableview - UITableViewDiffabledatasource NSFetchedResultController:发生变化时如何更新
- xml - 使用交叉引用计算 XML 中的元素
- selenium - 尽管在测试方法上设置了名称 @Test(testName="Browser"),但 TestNG 侦听器中的 ITestResult.getTestName() 返回 testname = null
- ios - 使用 AudioKit 模拟动态播放头(scratching / tunrtablism)
- python - 在 Pandas 中对数字进行排序时索引变得无序
- c# - 带有 IStringLocalizer 的单元测试类
- c# - 使用 c# 和 pdfiumviewer 库将 pdf 保存为 jpeg