2

我正在尝试为接受表单助手对象的助手方法编写测试,有没有办法在测试中创建表单对象?

/app/views/blahs/edit.html.erb

<% form_for :blahs do |blah| %>
 <%= my_helper_method(blah) %>
<% end %>

/app/helpers/blahs_helper.rb

def my_helper_method(废话)
  #
  # 废话是 ActionView::Helpers::FormBuilder
  # 在这里对表单对象做一些事情
  #
结尾

那么在我的测试用例中,如何创建表单对象?我仍在使用 Rails 2.3.9。

提前致谢

4

2 回答 2

0

尝试先使用它,然后再使用助手?除非您正在做一些捏造的“模型”类型的东西,否则部分是重用表单代码并且更容易测试的好方法。

帮助者在某种程度上用于“查看逻辑”。部分用于显示表单字段,即使是重复的字段。

它可以帮助您查看您在帮助者中尝试做的事情,而您在部分中无法做到这一点。

于 2011-02-10T22:24:11.173 回答
0

In theory you could require 'action_view/helpers/form_helper' (which lives in actionpack) in your helper spec which will make the method form_for available.

However I am with pjammer that you should question whether the logic you're putting in your helper_method really belongs there. If you tell us what you're trying to accomplish in helper_method we may be able to give you alternatives that don't require to pass the form object, which is probably a better approach.

于 2013-02-21T16:58:35.577 回答