views:

29

answers:

1

I was trying out Rails again, this time the 3 version, but I got stuck while writing tests for an action that I only call remotely.

A concrete example:

Controller

class PeopleController < ApplicationController
  def index
    @person = Person.new
  end
  def create
    @person = Person.new(params[:person])
    @person.save
  end
end

View (index.html.erb)

<div id="subscription">
  <%= form_for(@person, :url => { :action => "create" }, :remote => true) do |f| %>
    <%= f.text_field :email %>
    <%= f.submit "Subscribe" %>
  <% end %>
</div>

View (create.js.erb)

<% if @person.errors.full_messages.empty? %>
  $("#subscription").prepend('<p class="notice confirmation">Thanks for your subscription =)</p>');
<% else %>
  $("#subscription").prepend('<p class="notice error"><%= @person.errors.full_messages.last %></p>');
<% end %>

How can I test that remote form submission? I would just like to find out if the notice messages are being presented correctly. But if I try to do just

test "create adds a new person" do
  assert_difference 'Person.count' do
    post :create, :people => {:email => '[email protected]'}
  end
  assert_response :success
end

It will say that the "create" action is missing a template.

How do you guys usually test remote calls?

A: 

Could you just use the 'xhr' function instead of the 'post' function? An example can be found at http://weblogs.java.net/blog/2008/01/04/testing-rails-applications, if you search for 'xhr'. But even then, I'm curious, even with a remote call, don't you need to return SOMETHING? Even just an OK header?

jasonpgignac
Thanks jasonpgignac, that solves my major problem, not being able to submit the remote request. Somehow I could find that 'xhr' method. Works like a charm for "assert_response :success".Now I'm trying to find a way to test the results of the javascript executed. I present a different message if there were any errors on the submission or not. But assert_select doesn't let me query the HTML of the page, after I run the "get :index" and the "xhr" method. Any idea how to check if the right message was presented?
sdsantos
Well, to clarify that I understand what you're asking - are you trying to test the javascript that is being returned by your controller, as a result of the xhr call? because you can test the content of what's returned. But if you're trying to test a javascript that is being run on the page to see if the actual behaviour is functional that's more difficult, you can't tell it to execute a javascript and view the response. You might be better off using something like Selenium with Webrat, where it actually open a browser and dummies user behvaiour.
jasonpgignac
Yup, I wanted to test the result on the page after the javascript is executed. I'll check out those libraries. Thanks once again.
sdsantos
My pleasure sir! Otherwise, there are javascript specific test frameworks you could use as well, such as QUnit which would accomplish what you want, particularly if you have pretty complex javascript to take care of!
jasonpgignac