You are here: Browse Railsplugins Form Test Helper
form_test_helper is a Rails plugin that uses assert_select to verify and manipulate HTML forms. It solves the problem we’ve all run into where you change the form, but the test doesn’t break because you’re doing this test: post :create, :book => {:name => ‘Pickaxe’, :category => 1, :out_of_print => 0} assert_response :success
Instead, you can work with the below form: submit_form do |form| # or select by dom_id or url if multiple forms on the page form.book.name = ‘Pickaxe’ form.book.category.options => [[‘Programming’, ‘1’], [‘Self-help’, ‘2’], ...] form.book.category = “Programming” # Can set using the option label or the option value form.book.out_of_print.uncheck # Uncheck the checkbox end assert_response :success
...or simply: submit_form :book => {:name => ‘Pickaxe’, :category => ‘Programming’, :out_of_print => false}
What’s the benefit of this over post :create…? It uses the action of the form, it verifies that the form and the fields you specify are present and not misspelled, and it preserves any hidden or default values that may be in the form.
=== Installing
As an external: ./script/plugin install -x http://form-test-helper.googlecode.com/svn/form_test_helper/
Not as an external: ./script/plugin install http://form-test-helper.googlecode.com/svn/form_test_helper/
With piston: piston import http://form-test-helper.googlecode.com/svn/form_test_helper/
=== Features
==== Dan Kubb’s test example:
new_book = {
:name => 'Pickaxe',
:category => 'Programming',
:classic => true,
}
get :new
submit_form do |form|
form.book.update(new_book)
end
book = assigns(:book)
assert_kind_of Book, book
assert_valid book
new_book.each do |attribute,expects|
assert_equal expects, book.send(attribute)
end
=== Requirements This requires Rails Edge (greater than revision 7420).
=== Other VersionsSee http://code.google.com/p/form-test-helper for more information.
=== Acknowledgments
form_test_helper was inspired by the excellent work of choonkeat on hpricot_forms, which is based on the hpricot library. If you prefer hpricot over assert_select, give hpricot_forms a try!
It was authored by Jason Garber. It is currently being developed and maintained by Zach Dennis.
NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly