The maze book for programmers!
mazesforprogrammers.com

Algorithms, circle mazes, hex grids, masking, weaving, braiding, 3D and 4D grids, spheres, and more!

DRM-Free Ebook

The Buckblog

assorted ramblings by Jamis Buck

assert_select

2 January 2007 — 1-minute read

I’m going to be experimenting with a new regular feature called “TADFALAICKIU (EW, AH)”, which is an acronym for “Tip A Day For As Long As I Can Keep It Up (Except Weekends, And Holidays)”. In other words, I’ll be posting short little pointers and suggestions that I’ve found useful, as frequently as I can (hopefully every weekday).

To start things off, let me just introduce a little nugget of beauty that I discovered today. It’s been in edge Rails for a while, and others have blogged about it before. Stated simply, assert_select is a wonderful thing.

Consider the following monstrosity, coded using assert_tag:

1
2
3
  assert_tag :tag => "img",
    :attributes => { :src => "/0000/0001/logo.gif", :alt => "My Company" },
    :parent => { :tag => "div", :attributes => { :id => "logo_box" } }

Basically, all it does is search the response for an image tag with the given “src” and “alt” attributes, which happens to be enclosed within a “div” tag with an id of “logo_box”.

Now, behold it rewritten to use assert_select:


  assert_select "div#logo_box img[src=/0000/0001/logo.gif][alt=My Company]"

This is where it’s at people. Forget assert_tag, it’s ancient history. assert_select the future!

(Although assert_tag is not yet deprecated, it will be eventually, so after upgrading to Rails 1.2, you’ll want to make sure you’re using assert_select instead.)

Reader Comments

That’s lovely! As an aside, is there any way that you can stop your code snippets from spilling over into your sidebar?

Maybe something like Evan Weaver’s blog? http://blog.evanweaver.com/

Hover over the snippets to expand

I believe the snippets only spill over if you are using IE. If you are seeing the problem on non-IE browsers, let me know. The specifics.

The hover-over-snippets trick is interesting, but I kind of find it districting, myself.

My feelings on IE are well known, though, so if you are using IE, I’m afraid my advice to you is to switch browsers, because I won’t be expending any effort to support IE.

The code snippets spill over for me in Safari.

Related: http://lukeredpath.co.uk/2006/7/7/testing-your-rails-views-with-hpricot

Hpricot would be faster I guess, but wouldn’t be included in core as it’s a dependency (and a compiled one at that). Maybe somebody should make assert_select use Hpricot but only if it’s installed…

Yup, ok, I see the spillover happening in Safari. I’m not sure why it’s happening—Safari is generally better-behaved than that. I’ll see what I can figure out, though I can’t make any guarantees on when I’ll figure it out. :(

I just read about assert_select in the second edition of the AWDWR book. I’ve yet to try it, but it seems amazing. I’m looking forward to your daily coding tips.

Thanks for posting this Jamis, it inspired me to add support for assert_select and assert_tag to Watir on Rails. More here: http://redsquirrel.com/cgi-bin/dave/2007/01/03

I love the way assert_select leverages the CSS attribute selector syntax (http://www.w3.org/TR/CSS21/selector.html#attribute-selectors). It is this kind of attention to detail and incorporation of prior art that makes developers lives that much easier. Taking this realization further, does the assert_select syntax support the ~= and |= operators? It would be nice if it did for completeness.

Bill, according to this cheat sheet, it does. :)