I'm trying to test to see if an input field matches one of my factories where the field is empty.
我正在尝试测试输入字段是否与我的工厂中的哪个字段为空。
address => {:first_name => '', :last_name => ''}
When checking for what is in the input field I've been using this:
当检查输入字段中的内容时,我一直在使用它:
assert_select '#first_name[value=?]', address.first_name
Except this does not work if the first name is blank. I'll get this error and the test fails.
如果第一个名称为空,则不起作用。我会收到此错误,测试失败。
Expected at least 1 element matching "#first_name[value='']", found 0.
<false> is not true.
This makes sense because the code generated will not have the value attribute. Is there a better way to verify the value of an input field?
这是有道理的,因为生成的代码不具有value属性。有没有更好的方法来验证输入字段的值?
As of now to test for this I can check if the address field is blank then check if there is an input field without a value attribute. But this is messy and verbose.
到目前为止测试这个我可以检查地址字段是否为空,然后检查是否有没有值属性的输入字段。但这很麻烦而且冗长。
Example of a universal check that works but is lengthy:
通用检查的示例有效但很长:
if address.first_name.blank?
assert_select '#first_name[value]', 0
assert_select '#first_name[type=text]', 1
else
assert_select '#first_name[value=?]', address.first_name
end
Related Information I'm using:
Hpricot 0.8.1
Nokogiri 1.1.1
Rails 2.2.2
Thoughtbot-Shoulda 2.0.5
Webrat 0.4.1
相关信息我正在使用:Hpricot 0.8.1 Nokogiri 1.1.1 Rails 2.2.2 Thoughtbot-Shoulda 2.0.5 Webrat 0.4.1
1 个解决方案
#1
Maybe you can use:
也许你可以使用:
assert_select "#first_name" do
assert_select "[value=?]", address.first_name unless address.first_name.blank?
end
I don't think I can get it any shorter. If it is a recurring pattern in your test case, you could extract it to a custom assertion:
我认为我不能更短。如果它是测试用例中的重复模式,则可以将其解压缩为自定义断言:
def assert_has_value_unless_blank(selector, value)
assert_select selector do
assert_select "[value=?]", value unless value.blank?
end
end
assert_has_value_unless_blank "#first_name", address.first_name
#1
Maybe you can use:
也许你可以使用:
assert_select "#first_name" do
assert_select "[value=?]", address.first_name unless address.first_name.blank?
end
I don't think I can get it any shorter. If it is a recurring pattern in your test case, you could extract it to a custom assertion:
我认为我不能更短。如果它是测试用例中的重复模式,则可以将其解压缩为自定义断言:
def assert_has_value_unless_blank(selector, value)
assert_select selector do
assert_select "[value=?]", value unless value.blank?
end
end
assert_has_value_unless_blank "#first_name", address.first_name