Friday, November 2, 2012

Test the HTML sent from rails

Sending emails in Rails is a piece of cake when you follow the official mailers guide . However testing the html is a bit problematic.

You can use the assert_select_email or the usual assert_select methods. I prefer the second option, and my code looks like (Rails 3.0.X)

email = ActionMailer::Base.deliveries.last
body = email.html_part.decoded
 
node = HTML::Document.new(body).root
assert_select node, ':root' do
   #do your assertions here
end
 
It took me, however, some time to fix the error
NoMethodError: undefined method `assert_select' for

In Rails 3.0.X it is as easy as including the appropriate module (within you test class)  

include ActionDispatch::Assertions::SelectorAssertions 


For newer rails versions check the official guide of teh the assert_select method, and check which module is it defined in.

No comments:

Post a Comment