🖥
Determining the state of selected with Capybara + rspec: Rails
Write as follows.
expect(page).to have_select('book[id]', selected: 'ソフィーの世界')
Test example.
require 'spec_helper' feature '/books', type: :feature do before do visit book_path select 'ソフィーの世界', from: 'book[id]' #いちど選択状態にする end scenario "要素が選択状態になっている" do expect(page).to have_select('book[id]', selected: 'ソフィーの世界') # 選択状態を判定する end end
<select id="book" name="book[id]"> <option>我輩は猫である</option> <option>ソフィーの世界</option> <option>ガリバー旅行記</option> </select>
point
- Since Capybara determines "the state seen from the outside", it validates the contents (text seen by the user) of
<option></option>
rather than the value of<option vallue="">
. - Passing to have_select / select may be the value of
<select id="">
the value of<select name="">
. It will search for either. (Note that if the same name and id in the same page have the same name, an error will occur. There is no way to specify either id / name) - Therefore, I think that it is better to specify in the text of
<label for=""></label>
like this . - have_select is not a matcher that directly determines selected. Determine the overall state of
<select></select>
. Soselected
in order to determine, selected in option: to specify.
Original by
Capybara + rspec で selected の状態を判定する : Rails
About
チャットメンバー募集
何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。
公開日時
2019-04-16
Discussion