RSpec和Capybara:如何确定选择控件是否选择了特定选项?

时间:2012-11-27 23:58:11

标签: ruby-on-rails rspec capybara

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

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

<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

UPDATE ....再次刺伤它,但似乎忽略了阻止物品(它表示即使有错误的信息也很好 - 原因可能很明显):

  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

1 个答案:

答案 0 :(得分:5)

尝试如下

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