给定一个select控件,您希望在表单加载时选择适用的选项(通过明显地在控制器中为您的模型加水),如何在RSpec/Capybara集成测试中确定是否实际选择了正确的选项?
例如,给定以下select控件...
<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>...how您会在值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 + "' ]" }
endUPDATE....another尝试了一下,但它似乎忽略了块项目(它说即使有错误的信息也是好的-原因可能很明显):
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发布于 2012-11-28 09:25:46
请按如下方式尝试
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 }
endhttps://stackoverflow.com/questions/13595428
复制相似问题