specifying output of render call

View: New views
2 Messages — Rating Filter:   Alert me  

specifying output of render call

by Matt McNeil :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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


Re: specifying output of render call

by Pat Maddox :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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
_______________________________________________
rspec-users mailing list
rspec-users@...
http://rubyforge.org/mailman/listinfo/rspec-users