How to test tag attributes using assert_select
Wednesday, September 10th, 2008There’s undoubtedly a smarter way to do this, but I couldn’t figure it out. I’m doing a functional (controller) test for one of my rails pages. I want to make sure that the picture coming back is exactly 224 pixels high.
I couldn’t find a way to do this using the assert_select syntax, but this workaround seems to work:
def test_picture_height
assert_select "div.secondary div.lead_picture img[height]" do |height|
# pull the tag -- this gives you the whole img tag
@h = height.to_s
# extract the height
@h.gsub!(/.*height="(.*?)".*$/, '\1')
#run the test
assert_equal @h, "224"
end
end
This can be tightened up, but I left it explicit so that I could understand what I was going. The assert select makes sure that I’m setting the height. The block makes sure that it’s equal to 224.
Are there easier ways to do this?


November 24th, 2008 at 12:15 pm
Any reason this doesn’t work?
assert_select ‘image[height=”224″]’
See: http://labnotes.org/svn/public/ruby/rails_plugins/assert_select/cheat/assert_select.html