I noticed that it is quite common in Rails to render back text for js requests with the text embedded within a jquery method call to insert it into the DOM.
// javascript code
$.getScript("/some_url");
// rails partial code, to make things more clear I have added some simple html
$("#some_id").text(unescape_javascript('<div id="foo">bar</div>'))
My question is how do you perform assert_select, or the equivalent, within a functional test on response text like this?
//
class FooBarControllerTest < ...
test "javascript response" do
xhr :get, :new
// how could I test the div here
assert_select "#foo", "bar" // this doesn't work
end
end
** Updated code to make this more clear
i dont know how would you request it from rails but with ajax and jquery would go like this:
thing you need is eval, so just put your string that hawe to be runed as javascript inside of eval();
After some fiddling around this works for me. The trickiest part is cleaning up all of the delimited chars such as \t,\n and \ before creating the HTML::Document object.
With Rails 4.x, this works for me (the trick is to override the "document_root_element" method during the spec/test):