Peter Marklund

Peter Marklund's Home


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:

      assert_response :success

      assert_response :success

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

1 comment(s)


Pierre-Alexandre Piarulli said 2009-05-25 08:18:

Hi. I'm using form_test_helper to! it's a good thing. But since i upgrade to rails 2.3.2 It's did'nt work. for exemple when i make this. new_theme = { :title => 'mon theme', } get :edit, :id => themes(:theme_one).id submit_form 'form_for_theme' do |form| form.theme.update(new_theme) end i have. 3) Error: test_should_create_with_form(Admin::ThemesControllerTest): NameError: uninitialized constant ActionController::UrlEncodedPairParser /test/functional/admin/themes_controller_test.rb:78:in `test_should_create_with_form' /test/functional/admin/themes_controller_test.rb:77:in `test_should_create_with_form' any idea. Or do you know some other good plugins for doing the same job ? thank you for your blog. PierAlexndR