1

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.