Easier testing of forms: form_test_helper

Posted by Jason Garber Tue, 24 Oct 2006 23:59:00 GMT

With the form_test_helper Rails plugin, your functional and integration tests can work more like the browser. No longer do you need to feed params to an action:
post :create, :name => 'Pickaxe', :category => 1, :out_of_print => 0
assert_response :success
... and then watch it silently break when you change your form but forget to change the test. With form_test_helper, you just call up the form, change the fields you want, and submit it.
get :new
submit_form do |form|
  form['name'] = 'Pickaxe'
  form['category'] = "Programming"
  form['out_of_print'].uncheck
end
assert_response :success
...or simply:
submit_form :name => 'Pickaxe', :category => 'Programming', :out_of_print => false

This is advantageous because it uses the action and method of the form, 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, like in form_spam_protection.

Features

  • Can select_form by dom_id or url—or call without arguments if there’s only one form
  • Specifying a field name that doesn’t exist raises an exception
  • Fields that are selects (dropdowns) won’t let you set a value that’s not in its options
  • Selects can set using the option label or the option value
  • Inspect/verify the options for selects and radio buttons
  • Works with RESTful links and forms – :method => :put, :delete…
  • Checks for the presence of a submit button when you submit the form
  • Works in functional and integration tests
  • You can pass #submit a hash and it will update the form accordingly. Submitting a form with new values can be as simple as: select_form.submit {:username => 'bob', :password => 'opensesame'}
  • Assert presence of and follow links
  • See tests for more goodies!

Installing

./script/plugin install -x http://form-test-helper.googlecode.com/svn/form_test_helper/

Requires EdgeRails or the assert_select plugin

Bugs/Suggestions

You can e-mail me with my first and last initials at this domain or post “issues” on http://code.google.com/p/form-test-helper

Installing Ruby + Rails + SWIG + SVN on OS X from scratch

Posted by Jason Garber Fri, 25 Aug 2006 00:08:00 GMT

I just moved up from a PowerBook to a MacBook and had to rebuild my environment so I could work on Collaboa. For posterity’s sake, here’s what you have to do:

  1. Install Xcode
  2. Download Roll_Ruby_On_Rails.zip from http://sg.validcode.at/articles/2006/03/31/the-best-way-to-install-rails-on-os-x
    • Modify your ~/.bash_login as directed. Or .bash_profile or .bashrc or .profile or whatever you call yours.
    • Source that file
    • Run the script with sudo sh roll_ruby_on_rails.sh
    • Probably do a gem up -y to make sure you’re running the latest.
  3. Download and install swig (not really necessary for svn-ruby bindings, but is necessary for sqlite3 on OS X)
    $ ./configure --prefix=/usr/local
    $ make
    $ sudo make install
    $ swig -version # Verify it's installed and the correct version
  4. Download and install Subversion
    $ ./configure --prefix=/usr/local --with-openssl --with-ssl --with-zlib
    $ make
    $ sudo make install
    $ svn --version # Verify it's installed and the correct version
    $ make swig-rb
    $ sudo make install-swig-rb
    $ make check-swig-rb # Optional.  Expect errors for FS type 'bdb'
  5. sudo gem install prereqs for Collaboa: xhtmldiff, syntax, and whatever else you need (like sqlite3-ruby or mongrel, perhaps).

ri disambiguation page in TextMate

Posted by Jason Garber Mon, 19 Jun 2006 21:51:00 GMT

TextMate is a beautiful little application, but one tiny corner of it wasn’t working and I decided to track down a fix. When I looked for documentation (^H) on an ambiguous Ruby method name (like ‘sort’), the links in the disambiguation page didn’t work.

Allan, the developer, helped me out on his IRC channel. He said the path to ri isn’t in my PATH. He meant that it’s in my bash path, but for whatever reason, this path isn’t read for ri. He said I needed to put an entry in ~/.MacOSX/environment.plist. I copied and pasted from this page and now it works like a charm.

Reloadable mocked-out classes

Posted by Jason Garber Thu, 18 May 2006 22:39:00 GMT

If you have a “mock object” in test/mocks/development that requires the object you’re mocking, it won’t be reloaded each request like you’d expect in the development environment. That’s because you’re using require instead of load. Try instead:
load "#{RAILS_ROOT}/app/helpers/application_helper.rb"
Or if you really want to get fancy, use require_or_load.

Older posts: 1 2