Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to test RJS in RSpec Controller specs?

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.

like image 213
Matt Darby Avatar asked Jan 07 '09 15:01

Matt Darby


2 Answers

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.

like image 82
Toby Hede Avatar answered Oct 13 '22 05:10

Toby Hede


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

like image 4
Patrick Ritchie Avatar answered Oct 13 '22 06:10

Patrick Ritchie