Forum: Ruby on Rails assert_select with respond_to JS or xhr?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
F9c435e4a76eae5a3c7eaec5d3d73005?d=identicon&s=25 seb_guard (Guest)
on 2007-04-26 03:49
Hi everyone,

I am a bit confused with xhr? and respond_to.

I have the folloing code in my view to update the 'emails' ID
link_to_remote(image_tag("refresh"), :update => "emails",
    :url => { :action => "list_emails" })

in the controller side i have somthing like :
 respond_to do |type|
        type.html { render :action 'list_emails' }
        type.js { render :partial => 'list_emails' }

The partial returns html code such as
[..]<ul><li>email1</li></li></ul> [..]

In my controller functional test
I do
xhr :get,:list_emails
assert_select "ul"

But `assert_select' did not find anything.
However if i changed my controller action (remove the respond_to and
replace with xhr?)
if request.xhr?
  render :partial => 'list_emails'
  render :action 'list_emails'

Then the assertion works perfectly.
Am i missing something ? Are not we supossed to migrate to the
respond_to structure ? wht would be the recommendation for this
situation ? stick with xhr?

thanks a lot

58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris Taggart (christ)
on 2007-04-26 10:27
(Received via mailing list)
seb_guard wrote:
>  respond_to do |type|
> [..]
> else
> thanks a lot
> -sebastien
assert_select looks at the content-type returned and tests accordingly.
If it is js then it thinks it's an rjs type response. I thought this was
a bug, and filed a ticket when 1.2 was being finalised, but is
apparently expected behaviour, I think on the basis that respond_to is
asking what type of response the reqester wants.

However, it does make it tricky to test the above scenario (where you
want the Ajax response to be just a partial, which is quite a common
idiom  IMHO). The best I've come up with is matching the @response.body
with a RegExp

Hope this helps (a bit).

Stuff about old cars
F9c435e4a76eae5a3c7eaec5d3d73005?d=identicon&s=25 seb_guard (Guest)
on 2007-04-27 02:17
Thanks for your answer Chris!

I was going to use RegExp on @response.body, so I am happy that you see
it as an alternative as well!

This topic is locked and can not be replied to.