2

いくつかの HTML スニペット (ajax 呼び出しとして使用される) を提供するコントローラーの場合、次のようなビュー仕様があります。

it "should not contain html element" do
    render
    rendered.should have_selector('div')
    rendered.should_not have_selector('html')
end

私たちの ajax コンテンツ ローダーでは完全な html ページをレンダリングできないため、結果に html タグが含まれていないことを確認したいと思います。

今のところ、結果renderedは単純な div タグです。

puts rendered

につながる

"<div>Some text</div>"

ただし、テストは失敗します。

Failure/Error: rendered.should_not have_selector('html')
  Capybara::ExpectationNotMet:
  expected not to find css "html", found 1 match: "Some text"

私もそれを試しました

rendered.should_not have_xpath('//html')
rendered.should_not have_css('html')
rendered.should have_no_xpath('//html')
rendered.should have_no_css('html')

しかし、結果は同じままです。

htmlテキストの一致をチェックするのはなぜですか? のテストでも同じ結果につながります。divbody

4

2 に答える 2

2

This should not be possible using the capybara matchers, because capybara is using Nokogiri::HTML to generate the internal DOM structure, and this always tries to create a valid HTML representation, which also includes an html and body tag, if there is none. See here in their source.

If you want to do that, you could fork capybara and change that line into something like

native = Nokogiri::HTML::Fragment(native) if native.is_a?(String)

Nokogiri::HTML::Fragment will not try to enhance the code to have an completed html structure.

Another solution would simply be doing a string match:

rendered.match('<html').should be nil
于 2013-05-15T11:45:50.937 に答える
0

この確認方法はどうでしょうか。(そして、アサートする前にページが適切に読み込まれることを確認するために、wait_until を投入します)

assert_false wait_until { page.has_selector?('html')} , "Found some stuff when it shouldn't have.."

私はこれを使用しています...

宝石「カピバラ」、「1.1.4」

于 2013-05-10T16:04:12.007 に答える