Reputation: 116
How generate it test from array
describe "some test" do
let(:some) { generated_array }
# raise error - undefined local variable or method
some.each do |key|
it "#{key} test" do
true
end
end
# will work
[1,2,3].each do |key|
...
end
How it can be realyzed with RSpec?
Upvotes: 1
Views: 398
Reputation: 6971
You can't have your test using the let
in the outer context that way due to RSpec being a DSL. RSpec reads the example spec files first, before running the tests. It hits the some.each
during DSL parsing before any of the actual tests are run.
This errors because some
gets defined on the example object, but the describe
and context
run in the an example group object context.
You can see this with:
describe 'thing' do
p self.ancestors
#=> [#<Class:0x007fa97a0761f8>, RSpec::Core::ExampleGroup, RSpec::Matchers,
# RSpec::Core::MockFrameworkAdapter, RSpec::Core::SharedExampleGroup,
# RSpec::Core::Pending, RSpec::Core::Extensions::InstanceEvalWithArgs,
# RSpec::Core::ExampleGroup::LetDefinitions,
# RSpec::Core::ExampleGroup::NamedSubjectPreventSuper,
# RSpec::Core::MemoizedHelpers, Object, PP::ObjectMixin, Kernel,
# BasicObject]
it { p selfs }
#=> #<RSpec::Core::ExampleGroup::Nested_1:0x007f8d1b397790 ...>
end
Upvotes: 1