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 回答