Peter Marklund

Peter Marklund's Home

Thu May 24 2007 02:14:53 GMT+0000 (Coordinated Universal Time)

Rails Testing: The form_test_helper Plugin

I've been using the form_test_helper plugin in my integration tests lately and I think it's a great way to increase test coverage of your views. The form_test_helper uses the assert_select CSS-like selectors and makes it easy to click links and submit forms and thus simulate something that comes pretty close to manual testing in the browser, unless of course you need to test AJAX functionality, in which case you need to look at Watir or Selenium. Here is some sample code:

      select_link("/admin/users").follow
      assert_response :success

      select_link("/admin/users/new").follow
      assert_response :success

      service_ids = [3, 4, 5]
      self.user_email = user_email
      assert_difference User, :count do
        submit_form do |form|
          form.user.update({
            :name => 'Test user',
            :email => user_email,
            :customer_id => 3,
            :password => "test",
            :services => service_ids
            })
        end
        assert_response :redirect
        assert_valid assigns(:user)
        assert_equal(Service.find(service_ids), assigns(:user).services)
        follow_redirect!
        assert_response :success
      end