gpt4 book ai didi

ruby-on-rails - RSpec 和 capybara : how to determine if a select control has a specific option selected?

转载 作者:行者123 更新时间:2023-12-04 06:20:59 25 4
gpt4 key购买 nike

给定一个选择控件,您希望在表单加载时选择适用的选项(显然通过在 Controller 中混合模型),您如何在 RSpec/Capybara 集成测试中确定是否实际选择了正确的选项?

例如,给定以下选择控件...

<select class="select optional" id="receiving_tally_po_id" name="receiving_tally[po_id]">        
<option value="10020">PO-10020 - Schoen Inc</option>
<option value="10018" selected="selected">PO-10018 - Senger, Eichmann and Murphy</option>
</select>

...您将如何在值 10018 上测试 selected="selected"?

describe "auto-populate with PO information" do
let!(:po) { FactoryGirl.create(:po) }

before { visit new_receiving_tally_path(:po => po) }
# The following checks to see if the option is actually there, but not if it's selected.
it { should have_xpath "//select[@id = '#receiving_tally_po_id']/option[@value = '#" + po.id.to_s + "' ]" }
end

更新....另一个尝试,但它似乎忽略了 block 项目(它说即使有错误信息也很好 - 原因可能很明显):

  it "should have the proper PO selected" do
should have_selector("select#receiving_tally_po_id") do |content|
content.should_not have_selector(:option, :value => po.id.to_s, :selected => 'selected')
end
end

最佳答案

尝试如下

it " should have your_value when page loaded" do 
find_field(field).value.should eq "your_value"
end

希望对你有帮助

适合上面的例子:

subject { page }
...
describe "auto-populate with PO information" do
let!(:po) { FactoryGirl.create(:po) }
before { visit new_receiving_tally_path(:po => po) }
it { find_field('receiving_tally_po_id').value.should eq po.id.to_s }
end

关于ruby-on-rails - RSpec 和 capybara : how to determine if a select control has a specific option selected?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13595428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com