Ruby Forum RSpec > specifying output of render call

Posted by Matt McNeil (Guest)
on 06.05.2008 02:18
(Received via mailing list)
Hello again,

First of all, thanks much for everyone's input on my question last week
about spec'ing an invocation of super.

Here's another question that seems resistant to my Google searching:

With this helper method (defined in a module and included in Rails
controllers):

def render_rjs_redirect(url = '/')
  render :update do |page|
    page << "location.href='#{url}'"
  end
end

Is it possible/advisable to specify either the block passed to render or 
the
output of the helper, something like below?  The only way I have been 
able
so far to spec this was to create a FooController with an index action 
that
called the method and then checked the response.body, but that seems 
like a
lot of overhead to test such a simple method.  Is there a better or 
obvious
way to do this?

Thanks!
Matt

it "should render javascript to redirect the browser" do
  self.should_receive(:render).with(:update) { |page| page <<
"location.href='example.com'"}
  render_rjs_redirect('example.com')
end

it "should render javascript to redirect the browser" do
  render_rjs_redirect('example.com')
  response.body.should == "location.href='example.com'"
end


--
View this message in context: 
http://www.nabble.com/specifying-output-of-render-call-tp17071496p17071496.html
Sent from the rspec-users mailing list archive at Nabble.com.
Posted by Pat Maddox (pergesu)
on 06.05.2008 04:48
(Received via mailing list)
Hey Matt,

One problem with rjs is that it's very difficult to test.  You can't
execute it, and executing code is necessary for specification by
example.

In this case, I would probably not unit test that code, based on the
combination of it being simple code but difficult to test.  I would
probably right a "ping" test though, that just makes the request and
asserts an ok response.  That at least covers syntax errors or bad
method calls.  You could write a selenium test if you want I suppose.

My current favorite pattern for RJS-type stuff is to push the behavior
into the client and have the server just render JSON.  This makes it
very easy to test the behavior using jspec, and leaves you with a very
natural separation of concerns.

Pat