【发布时间】:2013-05-10 14:14:15
【问题描述】:
对于提供一些 html sn-p(用作 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 与div内部 中的文本匹配?即使是对body 的测试也会得出相同的结果。
【问题讨论】:
-
在测试运行时尝试
puts rendered查看实际值 -
已经这样做了,我在我的文字中提到了它(可能不够清楚):“Some text” 最有趣的 - 如我所见 - 事实是,它在文本中找到了 html。
-
我知道 Nokogiri 在进行任何解析之前会修复您传递给它的 html 以使其有效,其中包括将其包装到 html 标记中。我认为水豚在某种程度上做同样的事情。换句话说,
should_not have_selector('html')永远不会通过,即使您提供的初始 html 只是部分内容。 -
谢谢!至少,在 capybara 内部,我发现
Capybara::Node::Simple调用Nokogiri::HTML可能完全符合您的描述。现在想知道是否有办法通过使用不同的选择器、完全不同的方法或仅通过或指定一些参数来避免这种情况。
标签: ruby-on-rails-3 capybara rspec-rails