Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.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 rspec-如何在it描述中引用let变量_Ruby_Rspec_Let - Fatal编程技术网

Ruby rspec-如何在it描述中引用let变量

Ruby rspec-如何在it描述中引用let变量,ruby,rspec,let,Ruby,Rspec,Let,我有以下规格: describe "can translate" do let(:from){591} it "#{from}" do expect(Translator.three_digits(from)).to eq 'five hundred ninety two' end end 但是591是硬编码和重复的,我想消除它… 那么如何在it描述中引用from 我试着让(:from){591} 然后使用it“#{@from}”但这并没有显示它 我还尝试使用it“#{f

我有以下规格:

describe "can translate" do
  let(:from){591}
  it "#{from}" do
    expect(Translator.three_digits(from)).to eq 'five hundred ninety two'
  end
end 
但是591是硬编码和重复的,我想消除它…
那么如何在
it
描述中引用
from

我试着让(:from){591}

然后使用
it“#{@from}”
但这并没有显示它

我还尝试使用
it“#{from}”
,但这会产生一个错误
未定义的局部变量或方法
“from”表示#(namererror)
,因为它正在查找一个局部范围的变量

我可以用常量避免所有这些范围问题,即

describe "can translate" do
  FROM=592
  it "#{FROM}" do
    expect(Translator.three_digits(FROM)).to eq 'five hundred ninety two'
  end
end
当我得到一个eror时,我实际上得到了一个名为translator.rb的ruby文件,它可以翻译591(或者任何数字,关键是它可以打印出来,不像我对变量的所有尝试)。 ` 但这似乎是一个糟糕的方法。如果可能的话,我更喜欢避免常数,我想对一行中的几个值进行测试,所以我需要一些可以在不同情况下更改的东西,而常数似乎不合适

我以前也尝试过:都使用局部变量和实例变量,但没有成功


如果我硬编码它,字面上把591作为文本,测试失败,然后591打印出来,这就是我想要的。但是,通过我在测试中使用的任何变量,我无法获得相同的结果。

如果要对多个值运行相同的测试,可以执行以下操作:

values = [100,200,300]

values.each do |value|
  it "#{value} do
    ... # use value here
  end
end
您无法按尝试的方式执行此操作的原因是
it
是一个类方法,并且
定义一个实例方法。还要注意的是,如果多次使用
let
,您将用新的方法定义覆盖以前的方法定义。由于rspec首先读取所有测试定义,然后执行它们,因此它们都将使用let定义的相同方法运行。因此,这不会如预期那样起作用:

values = [100,200,300]

values.each do |value|
  let(:from) { value }
  it "#{value} do
    puts from
  end
结束

上面将输入300三次。

我正在尝试(并成功地使用)如下局部变量:

...
describe "can translate" do
  from=738
  it from do
    expect(Translator.three_digits from).to eq 'seven hundred thirty eight'
  end 
end 
describe "can translate" do
  from=592
  it from do
    expect(Translator.three_digits(from)).to eq 'five hundred ninety two'
  end
end
...

您可以在
descripe
块中添加Ruby代码来定义一个集合,然后可以枚举该集合以生成多个示例,例如:

describe "can translate" do
  translations = [
    {input: 591, output: "five hundred ninety one"},
    {input: 592, output: "five hundred ninety two"}
  ]
  translations.each do |t|
    context "when input is #{t[:input]}" do
      it "translates to #{t[:output]}" do
        expect(Translator.three_digits(t[:input])).to eq t[:output]
      end
    end
  end
end
describe "can translate" do
  subject { Translator.three_digits(from) }
  let(:from){|e| e.description.to_i}
  it "592" do
    is_expected.to eq 'five hundred ninety two'
  end
  # or
  specify("593"){ is_expected.to eq 'five hundred ninety three' }
end