Form Test Helper Plugin

March 30th, 2007

This week I found the FormTestHelper plugin by Jason Garber. It both enhances and simplifies form testing in Rails functional and integration tests. The great thing is the FormTestHelper tests the form, not just the submission of the form data. For example, if you try to set a form element that doesn’t exist, an error is thrown.

Here’s my first pass at using FormTestHelper:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
assertions = proc do |response|
  assert_response(response)
  assert(exhibit = assigns(:exhibit), 
   "Should have assigned :exhibit")
  assert(exhibit.errors.empty?, 
   "@exhibit should not have errors: #{exhibit.errors.inspect}")
end

exhibit_count = Exhibit.count
get(:new)
assertions.call(:success)

submit_form('new_exhibit') do |f|
  f.exhibit.title = "New Exhibit"
  f.exhibit.exhibit_type_id = 1
  f.exhibit.license_id = 1
  f.exhibit.annotation = "Exhibit notes."
end

assert_equal(exhibit_count += 1, Exhibit.count )
assertions.call(:redirect)
assert_redirected_to(edit_exhibit_path(assigns(:exhibit)))
assert(flash[:notice])

Sorry, comments are closed for this article.