Ruby on rails 如何将变量添加到";它";rspec中的声明?

Ruby on rails 如何将变量添加到";它";rspec中的声明?,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,如果我有 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 v

如果我有

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)
'block in'中的
:未定义的局部变量或方法#的'analysis_file_1'(NameError)

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

您必须使用符号:

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
我无法让塞文塞卡特的建议发挥作用。这:

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

可以定义如下所示的常数:

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

您是否尝试将
analysis\u file\u 1
声明为本地?您的“不工作”打印出来的是什么?@rogerdpack,``不,这只是打印“analysis\u file\u 1”而不是值。@ScottWilson,argh。你是对的。很抱歉,我误解了我的结果。