Pages loaded in before scripts outside describe block will be missing any local let data

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the testing category.

Last Updated: 2024-11-23

I had the following code and I expected the user to see some law disciplines in their dashboard when the logged in.

 before do
   login(user)
 end

 describe 'changing profile settings' do
   let!(:administrative_law) do
     FactoryBot.create(:law_discipline, name: 'Administrative Law', level: 'LLB')
   end
   let!(:contact_law) do
     FactoryBot.create(:law_discipline, name: 'Contract Law', level: 'LLB')
   end

   it "works" do
     # psuedo-code
     expect(current_page).to have(2.law_disciplines) # fails

   end
end

This didn't work. The reason why is because I logged in outside this describe block. At this point the contact_law and administrative_law objects had not been created, so there's no way they could be reflected on screen (since the browser has already made its move).

Solutions: