I’m trying to use rspec to test for selecting a value from the select tag. The select tag should list all names of every user in the database. It works when I navigate through the site. But the test doesn’t seem to see the FactoryGirl-created user names:
The error is:
cannot select option, no option with text 'Person 4' in select box 'receiver_id'
Here is a portion of the message_pages spec:
let(:user) { FactoryGirl.create(:user) }
let(:other_user) { FactoryGirl.create(:user) }
before do
select other_user.name, :from => 'message[receiver_id]'
fill_in "Subject", with: "Hello"
fill_in "Body", with: "It's time to have fun!"
end
Here’s the HTML output of my view:
<label for="message_receiver_id">Receiver</label>
<select id="message_receiver_id" name="message[receiver_id]"><optionvalue="12">John</option>
<option value="13">Tom</option>
<option value="9">Jay</option>
<option value="14">Bob</option></select>
This looks like a lazy evaluation problem, common when using
let. From the documentation:So both
userandother_userwill only be created when you first call them, and if you have already rendered the page then it’s too late. Try switching theletcalls tolet!and see if that fixes things.(It’s funny though that you say that switching to
user.nameworks. Can you post what other initialization code is in the spec?)