Rspec和capybara选择了价值

Gan*_*row 5 ruby rspec ruby-on-rails capybara

我想断言,如果某些选择具有特定值而不是文本.如果我通过文本断言,这是有效的:

it 'should be true' do
   should have_select("country", :selected => 'Brazil')
end
Run Code Online (Sandbox Code Playgroud)

我的选择html是这样的:

<select name="user[country]" id="country">
  <option value="BR">Brazil</option>
 ...
</select>
Run Code Online (Sandbox Code Playgroud)

我想断言页面是否有选择值的选择,我该怎么做?

Jus*_* Ko 6

我不相信有一个内置匹配器用于按值检查选择列表.

但是,您可以使用以下value方法获取选择列表的值(即选定选项的值):

find(:css, 'select#country').value
Run Code Online (Sandbox Code Playgroud)

您可以使用expect语法将其与期望值进行比较:

expect( find(:css, 'select#country').value ).to eq('BR')
Run Code Online (Sandbox Code Playgroud)

或者使用should语法:

find(:css, 'select#country').value.should == 'BR'
Run Code Online (Sandbox Code Playgroud)