In my RSpec test, I have defined multiple methods using let() syntax. I have encountered a scenario where I need to use variable defined inside a let statement into another let statement inside same describe block.
context 'Reuse the name of first car in the second car' do
it_behaves_like 'some car test' do
let(:car1) do {
name1: "#{testcase['Car']}_#{Time.now.round}",
car_model: testcase['Toyota']
}
end
let(:car2) do {
name2: name1
}
end
end
end
As you can see I want to use the exact name value I defined name1 inside :car1 for name2 inside :car2. The above syntax throws me following error
NameError:
undefined local variable or method `name1' for #<RSpec::
How do I use exact value of name1 in :car2? Any ideas..
So
name1is also now a lazy variable to initialized when is called, if not for car1, then for car2.If the Time.now is a problem, you can leave the value of name1 as
testcase['Car']and then just interpolate the value of Time.now.