Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3879678
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:45:15+00:00 2026-05-19T22:45:15+00:00

I noticed that it is quite common in Rails to render back text for

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T22:45:16+00:00Added an answer on May 19, 2026 at 10:45 pm

    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.

    # wrapper method around the native assert_select method that extracts out the html
    # from raw html text that is embedded within javascript code.
    # ex.
    #   $('body').append("<div id=\"edit_post_form_container\" class=\"active modal_form_container\">
    #     <a href=\"#\" class=\"close\">
    #       <img alt=\"Close\" height=\"16\" src=\"/images/close.png?1293730609\" width=\"16\" />
    #     <\/a>
    #     <form accept-charset=\"UTF-8\" action=\"/posts/1023110335\" .../>")
    #
    # assert_js_select "input[type=?][name=?]", "text", "foo[bar]", :count => 1
    # assert_js_select "textarea", "Some long text"
    # assert_js_select "textarea[name=?]", "post_text", "Some long text"
    # assert_js_select "textarea[name=?][class=?]", ["post_text", "css_class", "Some long text"
    def assert_js_select(*args, &block)
      extracted_text = @response.body.match(/(\<.*\>)/)[0].
        gsub("\\n", "").gsub("\\t", "").gsub(/\\"/, '"').gsub("\\", '')
      if extracted_text.present?
        doc = HTML::Document.new(CGI::unescapeHTML(extracted_text)).root
        args.insert(0, doc)
        assert_select(*args, &block)
      else
        assert false, "Unable to extract any html from the js code."
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've noticed that there seems to be quite a bit of hostility towards Linq
I use remote desktop quite a bit and I've noticed that on one of
I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but
When experimenting with (embedded) Apache Derby DB, I noticed that a fresh database, with
I am sure that this must be quite a common problem so I would
I noticed that using an NSDateFormatter can be quite costly. I figured out that
On my current project I've noticed that IE uses a quite a lot of
Quite new to RIA Services but i am progressing... I noticed that some methods
I am quite new to grails. I just noticed that variables in the controller
I log quite a bit of stuff, and have noticed that Eclipse Console makes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.