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.
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
def options_for_user(user_id)
self.where(user_id => 1).collect{|m| [m.full_name, m.id]}
end
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 ...
精彩评论