0

我正在用 RSpec 和 Capybara 编写请求测试。我有一个将表单字段名称映射到预期值的哈希。

如何轻松检查每个表单字段是否具有预期值?

到目前为止,我正在这样做,但它很复杂且无法维护。在这种情况下,我也只考虑两种输入控件(选择框和其余部分):

expected_data = { 
  "address" => "Fake st 123",
  "city" => "Somewhere",
  "email" => "whoknows@example.com",
  "gender" => "Male",
  "state" => "FL",
}

select_boxes = ["gender", "state"]
# check for the select boxes
expected_data.select {|k,v| select_boxes.include?(k)}.each do |name, expected_value|
  page.has_select?(name, :selected_value => expected_value).should == true
end 
# check for the input fields
expected_data.reject {|k,v| select_boxes.include?(k)}.values.each do |expected_value|
  page.should have_css("input[value=\"#{expected_value}\"]")
end 

一行中是否有宝石或其他东西可以做到这一点?

4

1 回答 1

5

我发现以下内容更易于维护:

describe "form" do
  subject {page}

  before { visit "/path/to/form" }

  it { should have_field("address", :with => "Fake st 123") }
  it { should have_select("gender", :selected => "Male") }
  # And so on ...
end
于 2012-07-13T23:50:27.033 回答