Test Driven RJS with ARTS
Posted by Giovanni Intini | Filed under Ajax, Programming, Ruby, Ruby on Rails, Testing
If you’re like me, you like writing tests for your code. Writing tests is a nice thing to do and saves you a lot of headaches.
Writing tests for Ruby on Rails is really simple, with the wonderful testing framework built in Rails, but things become less ideal when you need to write tests for your RJS code.
This morning I wanted to test the following snippet:
def add_file_field render :update do |page| page.remove "link_to_add_#{params[:attachment_type]}" page.insert_html :bottom, params[:attachment_type], :partial => "#{params[:attachment_type]}_file_field", :locals => {:index => params[:index].to_i} end end
But the test was failing and I didn’t know why. The ARTS Plugin came to the rescue, allowing me to test the rjs response:
def test_add_file_field xhr(:get, :add_file_field, :index => 1, :attachment_type => "plan") assert_response :success assert_rjs :remove, "link_to_add_plan" # more code here... end
Unfortunately that didn’t help with my problem since the cause was not the RJS, I was simply forgetting to login in my setup method