# Assert\_select with respond\_to JS or xhr?

**URL:** <https://www.ruby-forum.com/t/assert-select-with-respond-to-js-or-xhr/92966>\
**Category:** Rails\
**Created:** [April 26, 2007, 3:49am UTC](https://www.ruby-forum.com/t/assert-select-with-respond-to-js-or-xhr/92966 "2007-04-26T03:49:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![seb\_guard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@seb\_guard](https://www.ruby-forum.com/u/seb_guard)\
**Post date:** [April 26, 2007, 3:49am UTC](https://www.ruby-forum.com/t/assert-select-with-respond-to-js-or-xhr/92966/1 "2007-04-26T03:49:37Z")

</div>

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’ }  
end  
[…]

The partial returns html code such as  
[…]

- email1

 […]

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’  
else  
render :action ‘list\_emails’  
end  
[…]

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

-sebastien

---

<div class="post-metadata">

**Author:** ![seb\_guard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@seb\_guard](https://www.ruby-forum.com/u/seb_guard)\
**Post date:** [April 26, 2007, 10:27am UTC](https://www.ruby-forum.com/t/assert-select-with-respond-to-js-or-xhr/92966/2 "2007-04-26T10:27:23Z")

</div>

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).  
Chris

## –

[http://www.autopendium.co.uk](http://www.autopendium.co.uk)  
Stuff about old cars

---

<div class="post-metadata">

**Author:** ![seb\_guard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@seb\_guard](https://www.ruby-forum.com/u/seb_guard)\
**Post date:** [April 27, 2007, 2:17am UTC](https://www.ruby-forum.com/t/assert-select-with-respond-to-js-or-xhr/92966/3 "2007-04-27T02:17:28Z")

</div>

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!

-seb
