0

如果我有

let(:analysis_file_1) { "./spec/test_files/analysis1.json" }
...
it "should have message x" do

我想在“it”语句中打印出 analysis_file_1 的值。

但这不起作用:

it "#{analysis_file_1} should have message x" do

它出错了

in `block in <top (required)>': undefined local variable or method `analysis_file_1' for #<Class:0x007f865a219c00> (NameError)

有没有办法在 it 消息中使用 let 定义的变量?

4

2 回答 2

1

您可以像这样定义一个常量:

ANALYSIS_FILE_1 = "./spec/test_files/analysis1.json"

it "#{ANALYSIS_FILE_1} should have message x" do

或者,如果您有多个分析文件,您可以将它们放在一个数组中:

["./spec/test_files/analysis1.json", "./spec/test_files/analysis2.json", "./spec/test_files/analysis3.json"].each do |analysis_file|
  it "#{analysis_file} should have message x" do
    # Your spec here
  end
end
于 2013-08-10T05:52:07.283 回答
-3

你必须使用一个符号:

it "#{:analysis_file_1} should have message x"

==

正如 Peter Klipfel 建议的那样,声明一个局部变量对我有用:

analysis_file_1 = "./spec/test_files/analysis1.json"

it "#{analysis_file_1} should have message x" do

我无法让 Sevenseacat 的建议发挥作用。这个:

describe "Joe" do
  subject(:last) { "Smith" }

  it "should have message x"  do 
    pending
  end
end

产生了输出:

Pending:
  Joe should have message x
    # No reason given
    # ./spec/requests/static_pages_spec.rb:6

我预计输出是:

Pending:
  Joe Smith should have message x
于 2013-08-09T21:46:15.450 回答