I expect that what you want to do is isolate the logic of building something using FactoryGirl in a method and then using that to define various Rspec examples.
Here's a technique of doing so by creating a global method.
def how_about_a_method
2.times.map { FactoryGirl.build(:foo) }
end
describe 'lucys question' do
context 'accessing something' do
how_about_a_method.each do |item|
it 'should have an item' do
expect(item).to be
end
end
end
end
Note that you'll need to use FactoryGirl.build
because in your spec_helper you've specified to include the FactoryGirl syntax methods with config.include FactoryGirl::Syntax::Methods
but in this global method you're not in an RSpec class anymore so FactoryGirl hasn't been included. I tend to prefer to not include FactoryGirl as it specifies in the documentation, but will instead always reference FactoryGirl explicitly as I have done in the method here.
A more interesting question is "Why is @array
nil?". It helps to understand a little bit about how RSpec works. Inside your it
block is what is known as an example. This is evaluated in a different scope to the context
block (which is known as an ExampleGroup
). Attempting to use result of before
, let
or let!
in the ExampleGroup context doesn't work because they are also evaluated in the Example context, that is the same scope your it
block is evaluated in.
Obviously a global method is global so you can access it wherever you like.
Personally I think the scoping is the most complicated part of RSpec. I wish it were documented better.