如何在 Rails 3 中使用 HTML::selector?
我正在测试一个表格。它看起来像:
<form accept-charset="UTF-8" action="/the_action" method="post">
<select id="id" name="z[z_id]">
<option value="7">Foo</option>
<option value="11">Bar</option>
</select>
</form>
如何将所有选项值放入数组(7,11)中进行测试?这些值是模型的 ID,因此我想测试每个对象的某些属性。
我阅读了 HTML::Selector 的 api 文档(http://api.rubyonrails.org/),但没有帮助。
还使用了Rails Guides(http://guides.rubyonrails.org/testing.html#testing-views)中的assert_select,但没有弄清楚如何以测试相等性以外的方式使用值。
您有推荐的入门资源吗?
谢谢。
编辑:这是视图中的代码:
<%= form_tag( :action => :the_action) do %>
<%= select :model_name, :model_id, Model.where(:user_id => 1).collect{|m| [m.full_name, m.id]}, :selected => selected_value, :include_blank => false %>
<%= submit_tag "view model" %>
<% end %>
此功能没有控制器代码。
I am testing a form. It looks like:
<form accept-charset="UTF-8" action="/the_action" method="post">
<select id="id" name="z[z_id]">
<option value="7">Foo</option>
<option value="11">Bar</option>
</select>
</form>
How do you get all of the option values into an array(7,11) for testing? These values are id's for a model, so I want to test certain attributes for each object.
I read api docs (http://api.rubyonrails.org/) for HTML::Selector but it didn't help.
Also used assert_select from Rails Guides (http://guides.rubyonrails.org/testing.html#testing-views) but didn't figure out how to use value in a way other than testing for equality.
Do you have any recommended introductory resources?
Thanks.
Edit: Here's the code in the view:
<%= form_tag( :action => :the_action) do %>
<%= select :model_name, :model_id, Model.where(:user_id => 1).collect{|m| [m.full_name, m.id]}, :selected => selected_value, :include_blank => false %>
<%= submit_tag "view model" %>
<% end %>
There is no controller code for this functionality.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,好像您正在通过视图测试模型。有什么理由不只检查视图中选择器是否存在,并在模型测试中测试模型的属性?
编辑:
所以我处理这个问题的方法是在模型上创建一个方法
,然后我从视图中调用这个方法,并在单元测试中测试选项创建。也许这只是我的看法 - 但这使得测试这类事情变得非常容易......
Sounds to me as if you are testing the model through the view. Is there any reason not to just check for the presence of the selector in the view, and test the attributes of the model in the model tests?
Edit:
So the way I would deal with this would be to create a method on the model
and then I would just call this method from the view, and test the option creation in the unit tests. Maybe that's just me - but it makes testing this sort of thing very easy ...