On Fri, Jul 11, 2008 at 1:04 PM, Reggie Mr. [email protected]
page.replace_html :divid :partial => 'apartial'
@page = mock('page)
What’s the best practice to validate rjs responses?
well tested third party libraries) against helper methods which just use
regular expression matching underneath the covers (like ARTS or the
I like to let the framework and the third party libraries test that
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:
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
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
or the inline RJS grows it’s pretty simple to extract an RJS file after
When an RJS file becomes messy or difficult to understand you can
that into using Renderer objects. For more information see “Extract
from RJS” in the Rails Refactoring Catalog: