Topic: DRYing up view tests...
So if you've been following my posts, you've seen that I've been progressing from model->controller->view tests. Using a slew of custom assertions for model testing and the Test::Rails package for divorced controller and view tests.
I feel like I've been able to keep both model and controller tests rather clean and DRY through aggressive refactoring/custom assertions. However view tests seem to have some un-attractive repetition which so far appears relatively hard to clean up. And after staring at it some more, I'm realizing some of the problems are shared with the controller tests.
The easiest problem for me to explain, and probably easiest to fix:
In Test::Rail's view tests, many of the assertions take the form of
assert_something form_url, other_parameter(s)
Where the form_url is typically '/<controller>/<action>'.
So a relatively normal test looks like
def test_edit_new
assigns[:model_object]=ModelObject.new
render
form_url = '/models/create'
assert_post_form form_url
assert_field form_url, :text, :model_object, :field_name
assert_field form_urm, :text, :model_object, :other_field
assert_submit form_url, 'Submit'
end
I find the repetition of the 'form_url' everywhere very ugly. I'm also not too keen on the assert_post, assert_submit that tend to "wrap" every form test.
I think the solution will be to create an custom assertion that would be used as follows (Handwaving/prototype code follows)
def test_edit_new
assigns[:model_object]=ModelObject.new
render
assert_form '/models/create', :submit=>'Submit' {|f|
f.field :text, :model_object, :field_name
f.field :text, :model_object, :other_field
}
end
With the assert_submit, and assert_post_form handled internally to the assert_form, etc. This looks like the a standard rails idion too, see the create table migration syntax or some of the form builders, I beleive. Does this look like the right track to you? (Of course in addition to f.field, I'll need to delegate most of the current ViewTestCase assertions, as well as provide for the one-two that it seems to miss (textareas).
I'm also not sure if I should push :model_object up into the hashlist of the assert_form to remove it from the field definitions. Doing so would make the f.field call's nicer, but would limit the ability to work with a single form that has multiple model_objects attached....
I think I'll stop here -- the other place I'm feeling non-DRY is extremely hard to explain and until I understand it better it probably isn't worth trying to explain it to others...