[rspec-users] rspec render :update

Zach Dennis zach.dennis at gmail.com
Sun Jul 13 00:56:03 EDT 2008

On Fri, Jul 11, 2008 at 1:04 PM, Reggie Mr. <lists at ruby-forum.com> wrote:

> Hi all,
> I'm having trouble with rspec 1.1.4 and rjs
> In the controller:
> render :update do |page|
>        page << 'added text'
>      end
> render :update do |page|
>        page.replace_html :divid :partial => 'apartial'
> end
> spec:
> response.should have_rjs   #this should be successful for any rjs
> request; but fails
> response.should have_rjs(:replace_html)  #no luck
> I have tried mocking the page object, but that also fails.
> e.g
> @page = mock('page)
> @page.should_receive(:replace_html)
> What's the best practice to validate rjs responses?

I don't like validating generated JavaScript code from framework methods (or
well tested third party libraries) against helper methods which just use
regular expression matching underneath the covers (like ARTS or the built-in
Rails helpers).

I like to let the framework and the third party libraries test that their
generated JavaScript is correct and I like to make sure my code is just
doing the right thing using mocks.

The below is a spec that will ensure a "render :update" call inside a
controller action works...

describe SomeController, '#foo' do
  it "renders the items entry form" do
    page = mock("page")
    page.should_receive(:replace_html).with(:some_id, partial =>
    get :show, :id => 1, :expense_reimbursement_id => 2

And here's the controller method:

  def show
    render :update do |page|
      page.replace_html :some_id, :partial => "Items/form"

I like to keep my RJS separate from my controller as much as possible by
pushing the code into external RJS files. This helps keep the controller
simple and easily understandable. I know sometimes the controller action and
the RJS are both so simple that it seems like overkill to separate them.
This just becomes a judgment call by the developer. If the controller action
or the inline RJS grows it's pretty simple to extract an RJS file after the

When an RJS file becomes messy or difficult to understand you can refactor
that into using Renderer objects. For more information see "Extract Renderer
from RJS" in the Rails Refactoring Catalog:

Zach Dennis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20080713/6575c9bd/attachment.html>

More information about the rspec-users mailing list