Testing microformats in Rails applications with assert-microformats

Posted on

I've recently been doing a few things using Ruby on Rails. Being a proponent of microformats and increasingly a fan of test-driven development I wanted to write some tests to make sure that the microformats being produced by the Rails app I'm working on were all in the right places and contained the right information, so a couple of days ago I wrote a little Gem called assert-microformats.

You can include it in your Rails app by adding this line to your test environment config file (config/environments/test.rb):

config.gem "georgebrock-assert-microformats",
  :source => "http://gems.github.com/",
  :lib => false

Now you can test your microformats using some shiny new assertions.

By default these assertions will look for microformats in Rails' @response:

test "show view should contain an hcard" do
  get :show
  assert_mf_hcard
end

You can also test the values of microformat attributes:

assert_mf_hcard :fn => 'George Brocklehurst', :url => 'http://georgebrock.com/'

And you can pass in some HTML, which comes in handy when you're testing view helpers or not using Rails at all:

assert_mf_hcalendar my_html, :summary => 'Birthday party'

The microformat parsing is done using Mofo, so assert-microformats supports all the microformats that Mofo supports.

If you're interested in how it works, or you want to make any changes, the code is all on GitHub.

I'd also like to thank Tom Lea for looking over the code and making some improvements.

2 Comments

Attila Györffy commented on :

I had difficulties when I added your gem as a dependency inside the project (environment.rb):

I got the following error:
uninitialized constant Rails::Plugin::Test

Turns out it was missing the test cases, so I've just added the following line at the top of rails/init.rb:

require 'test/unit/testcase'

I also forked your project on GitHub. You can follow my changes/updates at http://github.com/liquid/assert-microformats

Thanks for your gem, I like the ideology.

George Brocklehurst responded on :

Thanks Attila, I've merged your changes into my repository on GitHub.