[RSpec][RJS] Problem with specing create.rjs in controller_spec


#1

Could somebody please explain me what I am doing wrong.
I am trying to spec create.rjs file, that is returned from my
controller.

render_template(“create.rjs”) works just fine,

however have_rjs fails with the following error:
‘CommentsController while posting a comment renders a create.rjs
template when comment is successfully created in ajax request’ FAILED
No RJS statement that replaces or inserts HTML content.

Thank you!
Evgeny

=====controller
def create
@comment.save

respond_to do |format|
      format.js
end

end

=====create.rjs
comment_id = "comment_"removed_email_address@domain.invalid_s

page[‘add_comment’].hide
page.insert_html :top, ‘comments_list’, :partial => “comments/
comment”, :locals => {:comment => @comment, :item => @item}
page.visual_effect :highlight, comment_id, :duration => 1

=====spec file
it “renders a create.rjs template when comment is successfully
created in ajax request” do
request.env[“HTTP_ACCEPT”] = “application/javascript”
post :create, :item_id => 1, :item_type => “Space”, :comment =>
{:content => “text”}

response.should render_template("create")
response.should have_rjs

end


#2

On Thu, Feb 26, 2009 at 10:01 AM, Evgeny Bogdanov
removed_email_address@domain.invalid wrote:

end
it “renders a create.rjs template when comment is successfully
created in ajax request” do
request.env[“HTTP_ACCEPT”] = “application/javascript”
post :create, :item_id => 1, :item_type => “Space”, :comment =>
{:content => “text”}

response.should render_template(“create”)
response.should have_rjs
end

Controller specs, by default, do not render templates. If you want to
specify the contents of a template from your controller spec, tell it
to integrate_views (hmmm - maybe we should change that to
render_views???)

describe MyController do
integrate_views
it “does something” do

end
end

HTH,
David


#3

On 26/02/2009, at 11:25 AM, David C. wrote:

template when comment is successfully created in ajax request’ FAILED
format.js

end

HTH,
David

I’d say it’s a matter of perspective. The name “integrate_views” is
suitable from the perspective of the views being integrated into the
execution stack. The name “renders_views” is suitable from the
perspective of identifying that a controller example group renders its
views.
-Nick


#4

Thank you David.
I decided to test rjs file separately of the controller, so I created
a spec for it and put it into views folder.
In rjs file the partial is called, however I would like to mock it.

  1. I tried to use in spec the line:
    template.should_receive(:render).with(:partial => “comments/
    comment”).and_return(true)
    but partial is still called.
  2. another approach would be to mock the methods of the “page” object,
    but I couldn’t find out how to do it.
  3. How do I make sure that “#add_comment” was made “hidden” after the
    partial was called?
    response.should have_tag("#add_comment") after “render :template”
    doesn’t work.

Thank you in advance and sorry for the long list.
Evgeny
PS. Are these questions covered in the book: “The RSpec Book:
Behaviour Driven Development with RSpec, Cucumber, and Friends”?

===== comment_create_rjs_spec.rb
describe “comment create rjs” do
it “should hide add_comment div” do
comment = mock_model(Comment, :id => “1”)

assigns[:comment] = comment
???template.should_receive(:render).with(:partial => "comments/

comment").and_return(true)
render :template => ‘comments/create.rjs’
end
end
===create.rjs
comment_id = "comment_"removed_email_address@domain.invalid_s

page[‘add_comment’].hide
page.insert_html :top, ‘comments_list’, :partial => “comments/
comment”, :locals => {:comment => @comment, :item => @item}
page.visual_effect :highlight, comment_id, :duration => 1


#5

Hi Evgeny,

I would like to encourage you to verify your expectations that the JS
is functioning in the browser, using tools like Webrat and Selenium.
IMO spec’ing RJS files provides little value in the grand scheme of
things, yet those specs build up a heavy maintenance overhead.

With RJS you are usually either generating javascript manually and
sending them to the “page” object, or you are using Rails API for
invoking things in javascript. Either way you’re unable to actually
verify the JS is doing what you want and there’s no design value
coming from things like, “page[:foo].hide” or "page <<
‘doSomething()’, and there’s no verification of the behaviour
expected.

A wonderful alternative is going the unobtrusive javascript route, and
keeping JS in JS files, and then having your app send back JSON or
having your RJS files merely make a function call to JS. Now when you
use Webrat + Selenium you’re able to ensure the JS you implemented is
doing what it should be doing on the page.

On Fri, Feb 27, 2009 at 6:10 AM, Evgeny Bogdanov
removed_email_address@domain.invalid wrote:

  1. How do I make sure that “#add_comment” was made “hidden” after the
    describe “comment create rjs” do
    comment_id = "comment_"removed_email_address@domain.invalid_s

No RJS statement that replaces or inserts HTML content.
end
=====spec file
Controller specs, by default, do not render templates. If you want to

HTH,
David


rspec-users mailing list
removed_email_address@domain.invalid://rubyforge.org/mailman/listinfo/rspec-users


rspec-users mailing list
removed_email_address@domain.invalid
http://rubyforge.org/mailman/listinfo/rspec-users


Zach D.
http://www.continuousthinking.com
http://www.mutuallyhuman.com