如何对JavaScript响应执行功能测试?

时间:2011-01-31 21:24:57

标签: ruby-on-rails ruby testunit

我注意到在Rails中为js请求渲染文本是很常见的,其中嵌入了jquery方法调用中的文本以将其插入到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>'))

我的问题是你如何在这样的响应文本的功能测试中执行assert_select或等效语句?

// 
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

**更新了代码以使其更加清晰

3 个答案:

答案 0 :(得分:2)

经过一番摆弄,这对我有用。最棘手的部分是在创建HTML :: Document对象之前清理所有分隔的字符,例如\ t,\ n和\。

# 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

答案 1 :(得分:0)

我不知道你会如何从rails请求它但是ajax和jquery会这样:

$.ajax({
        type: 'POST',
        url: 'getstring.php',
        data: '',
        success: function(server_response){
            if(server_response){
                eval(server_response);
            }
            else{

            }
        }
    });

你需要的是 eval ,所以只需把你的字符串作为javascript(eval()中的javascript;

答案 2 :(得分:0)

使用Rails 4.x,这对我有用(诀窍是在规范/测试期间覆盖“document_root_element”方法):

context "with assert_select" do
  def document_root_element
    Nokogiri::HTML::Document.parse(@html).root
  end

  it "works" do
    xhr :get, :new
    @html = response.body # or wherever the response HTML comes from
    assert_select "#foo", "bar" # should work now
  end
end