Rails give us the ability to write inline RJS via
render :update syntax, as in :
render :update do |page| page['addressPreviewStatus'].update 'Address Not Found' end
Previous code will update the content of tag with “addressPreviewStatus” id with ‘Address Not Found’.
But how can we spec that out ? I needed to search a little as there seems to be very little examples.
First in rails there is a special assertion
assert_select_rjs, merged from the assert_select plugin, it let you test your RJS with a syntax similar to RJS itself.
RSpecOnRails has a special matcher wrapping
You can use it on
response to specify what should be generated, for exemple you can :
# Specify response should contains an update or insert of some kind response.should have_rjs # Specify response should contains an update or insert for the tag with given id response.should have_rjs('id') # Specify response should contains a specific update, insert, etc. for the given tag response.should have_rjs(:replace, 'id')
You get the point. Now, a nice syntax allows you to write RJS this way :
render :update do |page| page['id'].update('replacement text') end
So my first try was to use :
response.should have_rjs(:update, 'id', 'replacement text')
but this fail miserably with an error Unknown RJS statement type update. I tried different syntax but none worked.
Finally browsing through source of
assert_select_rjs I found what I was looking for. When using this syntax you should use one of
:chained_replace_html depending what you want to test
Now here is the solution :
response.should have_rjs(:chained_replace_html, 'id', 'replacement text')