Testing Helpers in Rails

Posted by Peter Donald Wed, 13 Jun 2007 14:01:00 GMT

It can be useful at times to unit test helpers to make sure they generate correct html. It is not obvious how to do this at first. So far I have been testing my helper by defining a class “MyClass” at the top of my unit test and including all the appropriate modules. I also need to define a url_for method if I ever want to test helpers that generate links.

The code follows (Replace MyHelper with your appropriate helper class);

class MyClass
  include ERB::Util
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::UrlHelper
  include MyHelper

  def url_for(options)
    ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
    generated_path, extra_keys = ActionController::Routing::Routes.generate_extras(options, {})
    generated_path
  end
end

Then in my tests I do something like;

def test_revision_link
  assert_equal(
    "<a href=\"http://svn.sourceforge.net/viewvc/jikesrvm?view=rev&amp;revision=22\">22</a>", 
    MyClass.new.revision_link(22))
end

Seems easy enough to do in retrospect but things usually do.

Posted in  | 3 comments | no trackbacks

Enhanced Rails Plugin to Validate (X)HTML and CSS

Posted by Peter Donald Wed, 09 May 2007 05:17:00 GMT

It feels like forever ago that I was last working with ruby on rails. But I am back hacking away at a few rails and I slowly getting more familiarity with ruby itself rather than just using it as part of the rails platform.

Something I have wanted to do for a long time is automagically validate content such as (X)HTML, CSS, atom feeds etc that is generated by the web application. A while ago I put together the assert-valid-asset plugin that allowed you to assert (in functional tests) that the content generated is valid. However you still had to explicitly call the assert.

So recently I enhanced the plugin so that it can automatically validate generated content when configured to do so. To configure auto validation you need to set a class variable in Test::Unit::TestCase via code such as;

    class Test::Unit::TestCase
      self.auto_validate = true
    end

Then anytime content is generated in tests (such as via get and post methods) it will check the mime type of the content. If the content has a mime type of ‘text/html’ or ‘text/xhtml’ it will pass it to the ‘assert_valid_markup’ method. If the content is ‘text/css’ then it will be validated by the ‘assert_valid_css’ method.

Of course you may have tests that generate invalid (X)HTML or CSS (to work with specific unnamed browsers) and you may want to exclude these tests from the automatic content validation. This can be done by adding the test symbol to the exclude list or alternatively by adding the desired test symbols to an include list. Both of the following examples have identical behavior;

Example 1:
    class FooControllerTest < Test::Unit::TestCase
      ...
      self.auto_validate_excludes = [:test_foo,:test_bar]

      def test_foo; ... ; end
      def test_bar; ... ; end
      def test_baz; ... ; end
    end
Example 2:
    class FooControllerTest < Test::Unit::TestCase
      ...
      self.auto_validate_includes = [:test_baz]

      def test_foo; ... ; end
      def test_bar; ... ; end
      def test_baz; ... ; end
    end

You can grab it from subversion at;

svn checkout http://assert-valid-asset.googlecode.com/svn/trunk/

The original article describing the plugin is here .

Posted in  | no comments | no trackbacks

Smuggle

Posted by Peter Donald Tue, 14 Nov 2006 01:56:00 GMT

I needed to mirror some galleries from one smugmug account to another. I thought about doing it manually but that idea seemed like way too much work.

I found a python script by rutt that did a little bit of what I wanted and went to work hacking. This resulted in smuggle.py that may be useful if you ever have to programatically interact with the smugmug APIs. It will probably evolve as my needs change and as I actually start to learn python.

The source is available from subversion. Enjoy!

svn co http://www.realityforge.org/svn/code/smuggle/trunk/ smuggle

no comments | no trackbacks

Moving from Eyelook to Smugmug

Posted by Peter Donald Mon, 03 Jul 2006 05:33:00 GMT

I needed to host photos from a recent trip to New Zealand. I started modifying eyelook to make it easier to do what I wanted such as geocoding, bulk upload, hierarchial grouping, different sizes, different layouts, themes etc. It was taking a bit of time so I decided to look around at existing services but none of them suited my needs.

Then I found SmugMug and I was hooked. This is the best photo-sharing service I have seen. It seems to be aimed at professional photographers – there is no free version but you get what you pay for. It is far from perfect but there is no way I would consider anything else at this stage.

Luckily it has a referral system that gets you $5 off your first year if you indicate an account that got you onto the system so I got mine a little cheaper. I also helped out the friend that referred me by making it a little cheaper for them next year. If anyone wants to get same deal, it’s simple: enter my email address ( peter at realityforge dot org ) or my coupon ( EqCTz7hz7wkW2 ) in the ‘Referred by’ field on the signup form and you get $5 off.

Best photo sharing service yet!

no comments | no trackbacks

Eyelook: Rails photo gallery

Posted by Peter Donald Fri, 21 Apr 2006 04:55:00 GMT

Eyelook is a photo gallery I created over a weekend when I was home sick. It is amazing how productive rails can make you because within 8 hours I had the code in place and had set it up on my personal web server.

Very little of that time was spent coding – most of it was spent working on re-arranging the xhtml+css or figuring out how to go about Loading Binary Data into Rails Fixtures.

Every eyelook application has a set of users who have a list of albums with pictures. The image data is stored in the database and cached on the filesystem on demand. This way a backup of the database backs up the complete system.

An example of users page that lists galleries is

You can select one of these galleries and it will bring up a list of images such as

From there you can either download the original or can view a larger lightbox style image

The admin section is not as sexy but it is functional.

You can grab the source from subversion via the following command. Read the README.txt for installation instructions.

svn co http://www.realityforge.org/svn/code/eyelook/trunk eyelook

Posted in  | 8 comments | no trackbacks

Older posts: 1 2 3 ... 6