There are a few tricks that can be used to make this run even smoother. The first trick is to utilize assert_tag to check your HTML generation. assert_tag wants to check the body in the @response object which is expected to be a valid XHTML string. But you are not in the request/response environment of functional/integration tests, so @response is not available. Not to worry, we can just fake it out for now. Create a class with an accessible instance variable that is an instantiated string named body:
class Response
attr_writer :body
def initialize
@body = ""
end
def body
"<x>" + @body + "</x>"
end
end
The next trick involves testing the image_tag helper. If my helper generates one or more img tags, they can be hard to test with assert_tag. The reason for this is due to the change in Rails to help the various asset tags work better with browser side caches. The helper
image_tag "graphic.gif"
results in something similar:
<img src="/images/graphic.gif?12345678">
where the parameter after the ? is a file based timestamp. This helps the browser to decide to get a new copy of the asset or not. But calling assert_tag :tag => 'img', :attributes => {:src => "/images/graphic.gif?12345678"} is brittle, since if you update the timestamp, (say the next time you get an update from Subversion,) your tests will break. The easy solution to this is to force the ASSET_ID to always be a fixed string. Add this line to config/environments/test.rb:
ENV['RAILS_ASSET_ID']=12345
Now this will be constant in your tests:
def test_image_asset
@response = Response.new
@response.body = my_helper_that_returns_an_img_tag
assert_tag :tag => "img", :attributes => {:src => "/images/graphic.gif?12345"}
end
[1] http://nubyonrails.topfunky.com/articles/2006/04/07/test-your-helpers