Rails 3 and rspec - select from select list
Asked Answered
B

1

7

I want to select value from select list in RSpec. For example i have such data:

<div class="control-group">
 <label class="control-label" for="user_teacher_leader_attributes_teacher_id">Teacher names</label>
  <div class="controls">
   <select id="user_teacher_leader_attributes_teacher_id" name="user[teacher_leader_attributes][teacher_id]">
    <option value="1" selected="selected">Math teacher</option>
    <option value="2">Physics teacher</option>       
  </div>
</div>

I want to select option Physics teacher via RSpec. How can i do that? Also, can i choose something from the list by value (for example, select Physics teacher by value "2" which it has)?

Byington answered 7/4, 2012 at 15:6 Comment(2)
You need have_selector matcherUnemployment
@Mik_Die Why should i use matcher to select option? Are you sure?Byington
B
18

I found solution. You should use select method:

select "Physics teacher", :from => "teacher_leader[teacher_id]"
Byington answered 7/4, 2012 at 16:46 Comment(2)
Is there a way to make this work with select that has an option twice? (e.g. 'United States' is twice in my country-select to have some "priority countries" for better UX)Eurhythmy
This doesn't answer the question. Which sucks because I had the same question. How to select by value not option text?Leningrad

© 2022 - 2024 — McMap. All rights reserved.