3

给定一个选择控件,您希望在表单加载时选择适用的选项(显然通过在控制器中水合您的模型),您如何在 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

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

  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
4

1 回答 1

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
于 2012-11-28T01:25:45.957 回答