0

如何测试没有值的文本表单字段,例如以下情况:

<input class="string required" id="student_name" name="student[name]"
       size="50" type="text" />

我知道如果存在值,您将拥有:

<input class="string required" id="student_name" name="student[name]"
       size="50" type="text" value="John" />

您可以进行测试以确保表单字段预先填充了名称“John”,其中包含以下内容:

it "should display an enmpty field for the student name" do
  rendered.should have_selector("input", :type => "text",
                                         :name => "student[name]",
                                         :value => "John")
end

我尝试指定“:value => nil”,但没有奏效。

have_selector() 的文档非常少,因此无法在那里找到任何内容,也无法通过搜索找到任何示例(可能使用错误的术语进行搜索)。

根据建议,我也尝试过:value => '',但收到以下错误消息:

expected following output to contain a <input type='text' name='vendor[name]' value=''/>

当我尝试时,:value => nil我收到以下错误消息:

 NoMethodError:
   undefined method `include?' for nil:NilClass

答案
解决方案是使用:value => "":value => ""

它最初在我的案例中不起作用的原因是我在我的 Gemfile 中包含了 webrat 和 capybara gem。删除 webrat 后,它使用两种解决方案都有效。

4

2 回答 2

0

尝试将值指定为“”

it "should display an enmpty field for the student name" do
    rendered.should have_selector("input", :type => "text",
                                     :name => "student[name]",
                                     :value => "")
end
于 2012-05-04T14:31:20.013 回答
0

@Unixmonkey 在评论中所说的应该有效。你也可以这样做:

page.first(:css, "#student_name")[:value].should be_nil

编辑哦,我猜@Unimonkey 的评论根据您的更新不起作用,但以上内容会起作用 - 我已经测试过了。

编辑 2 回复:评论试试这个,因为你在一个视图规范中:

selector = css_select("#student_name").first
selector["value"].should be_nil
于 2012-05-04T14:32:05.880 回答