Search code examples
ruby-on-railsrspec-rails

RSPEC fails, doesn't initialize factory


In my app, project has_many another_project. I have the following test:

describe 'some test' do
    let(:project) { create(:live_project) }
    let(:another_project) { create(:another_project, :project => project ) }

    # before do
    #  another_project
    # end

    it 'does something' do
        expect ...
    end
end

It fails unless the commented code runs.

This seems strange because the line with another_project doesn't do anything. It seems as if the factory isn't properly initialized until something points to it.

What could be the issue that makes it fail/work with/without those commented lines?


Solution

  • It seems as if the factory isn't being properly initialised until something points to it.

    That's a feature. Lazy initialization, it's called. If a thing is not used, why do the work of creating it?

    Either use a let! instead of let, for things you want to always be created. Or create them in a before block.