Clicking Links in UnitTests

Using click /change/i is a good way of testing you links/methods, since it will assure your views render the valid link and you do not have to repeat the link parameters as in get :edit, :id=>3

  1. get form_test_helper with
    script/plugin install http://form-test-helper.googlecode.com/svn/form_test_helper/
  2. patch vendor/plugins/form_test_helper/lib/form_test_helper.rb
    -  if self["onclick"] && self["onclick"] =~ /'_method'.*'value', '(\w+)'/
    -    $1.to_sym
    +  if self["onclick"] && self["onclick"] =~ /\.method = '(.*)'/
    +    $1.downcase.to_sym
  3. insert helper to test/test_helper.rb
      def click(text)
        select_link(text).click
      end
  4. click click click clickaround!
      def test_should_not_upload_from_ftp
        login_as :aaron
        assert_invalid_upload do
          `cp -f test/fixtures/videos/test.mov #{ftp_accounts(:aaron).dir}/wrong_name.flv`
    
          click /upload.*ftp/i
          assert_response :redirect
          assert flash[:alert]
        end
      end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s