我試圖讓assert_select在一些原始文本上工作。以下是我的,但是當我執行選擇它說沒有匹配。我還連接從assert_select方法,其示出了受理HTML :: Node對象是可行的(I也曾嘗試包裹提取HTML中的HTML /體標記)的一個片段有沒有辦法在某些自定義HTML文本上使用assert_select?
# Controller test
test "edit form contains all required fields" do
group = groups(:a_group)
user = users(:group_member)
post = create_post(user, :group_ids => group.id)
xhr :get, :edit, {:id => post.id}, {:user_id => user.id}
node = convert_js_to_node(@response.body, "form")
puts node.inspect
assert_select node, "form"
assert_select node, "input[type=text]"
end
# test_helper.rb method
def convert_js_to_node(js, enclosing_tag)
regex = Regexp.new("(\<#{enclosing_tag}.*#{enclosing_tag}\>)")
HTML::Node.new(js.match(regex)[0])
end
## Test output
Loaded suite functional/posts_controller_test
Started
#<HTML::Node:0x1050e6040 @line=0, @children=[], @parent="<form accept-charset=\\\"UTF-8\\\" action=\\\"/posts/1023110311\\\" class=\\\"edit_post\\\" data-post-id=\\\"1023110311\\\" data-remote=\\\"true\\\" id=\\\"edit_post_1023110311\\\" method=\\\"post\\\"><div style=\\\"margin:0;padding:0;display:inline\\\"><input name=\\\"utf8\\\" type=\\\"hidden\\\" value=\\\"✓\\\" /><input name=\\\"_method\\\" type=\\\"hidden\\\" value=\\\"put\\\" /><\\/div>\\n\t\t<h3>Edit post<\\/h3>\\n\t\t<h5><img alt=\\\"Contact\\\" class=\\\"icon\\\" height=\\\"16\\\" src=\\\"/images/contact.png?1288409655\\\" width=\\\"16\\\" /> in some where<\\/h5>\\n\t\t\t\\n\t\t<div id=\\\"post_content\\\" class=\\\"clearfix clear\\\"
1) Failure:
test_edit_form_contains_all_required_fields(PostsControllerTest)
[]:
Expected at least 1 element matching "form", found 0.
<false> is not true.
# File actionpack/lib/action_controller/assertions/selector_assertions.rb, line 201
def assert_select(*args, &block)
# Start with optional element followed by mandatory selector.
arg = args.shift
if arg.is_a?(HTML::Node)
# First argument is a node (tag or text, but also HTML root),
# so we know what we're selecting from.
root = arg
arg = args.shift
elsif arg == nil
...
可能重複[你怎麼能執行功能測試JavaScript的響應?](http://stackoverflow.com/questions/4856295/how-can-you-perform-functional-tests-on-javascript-responses) – chris 2011-02-05 19:31:05