Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec and capybara selected value

I want to assert if certain select has certain value and not text. This works if I assert by text :

it 'should be true' do
   should have_select("country", :selected => 'Brazil')
end

My select html is like this :

<select name="user[country]" id="country">
  <option value="BR">Brazil</option>
 ...
</select>

I want to assert does page have select with selected value, how can I do that?

like image 572
Gandalf StormCrow Avatar asked Dec 06 '13 16:12

Gandalf StormCrow


2 Answers

I do not believe there is a built-in matcher for checking the select list by value.

You can however, get the select list's value (ie selected option's value) by using the value method:

find(:css, 'select#country').value

You could compare this to the expected value, using the expect syntax:

expect( find(:css, 'select#country').value ).to eq('BR')

Or using the should syntax:

find(:css, 'select#country').value.should == 'BR'
like image 163
Justin Ko Avatar answered Nov 01 '22 02:11

Justin Ko


Or if you prefer to stay away from CSS selectors:

find_field("country").value.must_equal "BR"

This assumes you name the label for this field country.

like image 24
hoffmanc Avatar answered Nov 01 '22 02:11

hoffmanc