我正在学习如何用黄瓜/ webrat编写测试.我的一个测试场景设置为测试表单验证(将字段留空).奇怪的是,我没有填写fill_in的字段被设置为字段的name属性.这只发生在我运行黄瓜时,使用浏览器时不会发生这种情况.
我正在使用的步骤是直截了当的:
When /^I submit the form$/ do
# Not filling in the 'Name' field here
fill_in 'Description', :with => 'This is a description'
click_button 'Save'
end
Run Code Online (Sandbox Code Playgroud)
运行使用上述步骤的方案后,我可以看到文本字段"Name"设置为"name"而不是为空.如果我用空格填充该字段,或者nil:
fill_in 'Name', :with => ''
Run Code Online (Sandbox Code Playgroud)
我正在测试的表单很简单:
<form action="/item/create" method="post">
<div>
<label for="ItemName">Name</label>
<input type="text" name="name" id="ItemName" />
</div>
<div>
<label for="ItemDescription">Description</label>
<textarea name="description" id="ItemDescription"></textarea>
</div>
<input type="submit" value="Save" />
</form>
Run Code Online (Sandbox Code Playgroud)
知道为什么会这样吗?
我还是Ruby的新手,基本上只是在完成Cooper的书之后编写我的第一个微程序.我被指向避免猴子修补的方向,但问题是我不知道有什么替代方法可以实现相同的行为.基本上,我想添加一个每个字符串对象都可以访问的新方法.明显的猴子修补方式是:
class String
def do_magic
...magic...
end
end
Run Code Online (Sandbox Code Playgroud)
我记得有一种使用String.send的方法.但我不记得它是如何完成的,也不记得我在哪里阅读它.任何人都可以指出任何仍然可以使该方法可用于String类和子对象的替代方法吗?