我正在用 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
一行中是否有宝石或其他东西可以做到这一点?