views:

865

answers:

2

What is the best way to ensure that the proper RJS is being generated in a Controller action?

For example, I want to ensure that a div is highlighted as such:

def new
  render :update do |page|
    page.visual_effect :highlight, :some_div
  end
end

Rant: This is quickly becoming one of the reasons I grow tired of RSpec after using it for a year. This should be an easy question, but it's one that no one seems to have an answer for.

I've been told repeatedly that RSpec specifies behavior and what I'm trying to do here is just "test code". Highlighting of the :some_div is behavior as far as I can tell.

+2  A: 

Is it not possible to setup a controller test for the output of the response and look for some javascript formatting?

  xhr :get, :new
  response.should be_success
  response.should have_text("... test for JS response ...")

I would also probably use Selenium to more fully test this on the client, and the Controller test is more of a "sanity" check.

Toby Hede
I have tried this, but I can only really test visual effects; not replace_html, etc. I can test that something is being replaced, but not the partial name, locals, etc.
Matt Darby
Yeah, it's not ideal ... I tend to push most of this testing out to Selenium and just keep the Controller tests at a very high level, testing the general application flow.
Toby Hede
+3  A: 

rspec gives you the have_rjs helper which wraps assert_select_rjs. Here are some specifics:

http://jonathan.tron.name/2007/11/24/rspec-and-inline-rjs

Unfortunately assert_select_rjs only covers:

:replace, :replace_html, :show, :hide, :toggle, :remove and :insert_html

So this won't handle the visual_effect from your question. However the ARTS plugin does support the visual effects.

http://github.com/timocratic/arts/tree/master

You should be able to combine that with rspecs new 'spec/interop/test'.

http://blog.davidchelimsky.net/2009/2/2/rspec-works-with-test-unit

Patrick Ritchie